Problems centering Image inside in a SettingsCard #459
-
I'm currently struggling with getting an Image to centre inside a SettingsCard, so I'm hoping someone can give me some pointers... I'm kind of new to the Community Toolkit controls, and tbh I'm kind of new to WinUI / Xaml in general so I may be trying to do something daft - let me know if there's better way of doing what I'm doing. Desired LayoutThis is what I'm hoping to do... I have a (arrowed lines just for illustration - not part of the UI) Actual LayoutHere's the Xaml I'm using, which is as close as I've been able to get to my desired layout.
However, this rendered as: ProblemsThe problems I have are:
This Any suggestions on what to try would be welcome... |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I managed to put a workaround together - it's not pretty but it works. The first part was to wrap the image in a <UserControl ... snip ... xmlns:tkcontrols="using:CommunityToolkit.WinUI.Controls" > <controls:SettingsGroup> <tkcontrols:SettingsExpander x:Uid="MouseUtils_MouseJump_Appearance" HeaderIcon="{ui:FontIcon Glyph=}" IsEnabled="{x:Bind ViewModel.IsMouseJumpEnabled, Mode=OneWay}" IsExpanded="False"> <tkcontrols:SettingsExpander.Items> <tkcontrols:SettingsCard x:Name="MouseUtils_MouseJump_PreviewImage" MinHeight="300" MaxHeight="300" Loaded="PreviewImage_Loaded"> <Grid MinHeight="283" MaxHeight="283" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Image Source="{x:Bind Path=ViewModel.MouseJumpPreviewImage, Mode=OneWay}" Stretch="None" /> </StackPanel> </Grid> </tkcontrols:SettingsExpander.Items> </tkcontrols:SettingsExpander> </controls:SettingsGroup> </UserControl> The second part was to inspect the source code for the CommunityToolkit library and find the names of the private / internal controls so I could use get references to them and hide them - this expands the "content" area to fill the entire private void PreviewImage_Loaded(object sender, RoutedEventArgs e) { bool TryFindFrameworkElement(SettingsCard settingsCard, string partName, out FrameworkElement result) { result = settingsCard.FindDescendants() .OfType<FrameworkElement>() .FirstOrDefault( x => x.Name == partName); return result is not null; } /* apply a variation of the "Left" VisualState for SettingsCards to center the preview image in the true center of the card see https://github.com/CommunityToolkit/Windows/blob/9c7642ff35eaaa51a404f9bcd04b10c7cf851921/components/SettingsControls/src/SettingsCard/SettingsCard.xaml#L334-L347 */ var settingsCard = (SettingsCard)sender; var partNames = new List<string> { "PART_HeaderIconPresenterHolder", "PART_DescriptionPresenter", "PART_HeaderPresenter", "PART_ActionIconPresenter", }; foreach (var partName in partNames) { if (!TryFindFrameworkElement(settingsCard, partName, out var element)) { continue; } element.Visibility = Visibility.Collapsed; } if (TryFindFrameworkElement(settingsCard, "PART_ContentPresenter", out var content)) { Grid.SetRow(content, 1); Grid.SetColumn(content, 1); content.HorizontalAlignment = HorizontalAlignment.Center; } } This results in the following, with the image properly centred in the otherwise-empty There's a danger this might break if the internals of the |
Beta Was this translation helpful? Give feedback.
-
Hey @mikeclayton , I ran into the same issue. I fixed it by setting Details:
Looking at the Properties Testing<toolkit:SettingsCard> <toolkit:SettingsCard.Description> <Grid MinHeight="300" Background="Black"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Background="Gray"> <TextBlock Width="500" Height="200" VerticalAlignment="Center" FontSize="20" Foreground="Black" TextAlignment="Center"> Content here </TextBlock> </StackPanel> </Grid> </toolkit:SettingsCard.Description> </toolkit:SettingsCard> |
Beta Was this translation helpful? Give feedback.
I managed to put a workaround together - it's not pretty but it works.
The first part was to wrap the image in a
Grid
andStackPanel
to allow it to centre property inside the "content" area of theSettingsCard
: