How to limit column height to others?

I have a grid in WPF that consists of 4 rows along 2 columns, where in column 1 is the Image control and column 2 is 4 text blocks. The problem is that the Image size control itself has the size of the image and expands the entry in the list too much (in the DataTemplate), and everything looks distorted. I do not want to manually set the maximum height / width, because I want the image to be the size of 4 text blocks located next to it. Any ideas?

<DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Image Source="{Binding Logo, Converter={StaticResource BSConverter}}" Grid.Row="0" Grid.RowSpan="4" Grid.Column="0" Stretch="Uniform" SnapsToDevicePixels="True"/> <TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="1"/> <TextBlock Text="{Binding Author}" Grid.Row="1" Grid.Column="1"/> <TextBlock Text="{Binding Version}" Grid.Row="2" Grid.Column="1"/> <TextBlock Text="{Binding Description}" Grid.Row="3" Grid.Column="1"/> </Grid> </DataTemplate> 

Thank you in advance

+4
source share
2 answers

You can use the Grid.IsSharedSizeGroup in the parent ListBox to make sure all your items get the same width for the first column, such as

 <ListBox ... Grid.IsSharedSizeScope="True"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition SharedSizeGroup="GroupA"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> ... 

For the image height problem, you can attach the height to the actual weight of the parent grid using FallbackValue 1.0 (to ensure that the image height does not affect the height of the grid)

 <Image Source="{Binding Logo, Converter={StaticResource BSConverter}}" Grid.Row="0" Grid.RowSpan="4" Grid.Column="0" Stretch="Uniform" SnapsToDevicePixels="True" Height="{Binding RelativeSource={RelativeSource AncestorType=Grid}, Path=ActualHeight, FallbackValue=1.0}"/> 
+2
source

StackPanel modifying your containers a bit to use the StackPanel in conjunction with the Grid and referring to the StackPanel binding through the ElementName , you must provide the visual effects you are looking for ...

 <DataTemplate> <Grid HorizontalAlignment="Left" VerticalAlignment="Top"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Image VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,0,8,0" Source="{Binding Logo, Converter={StaticResource BSConverter}}" Grid.Column="0" Stretch="Uniform" SnapsToDevicePixels="True" Height="{Binding ElementName=Contents, Path=ActualHeight}"/> <StackPanel VerticalAlignment="Top" Name="Contents" Grid.Column="1" Orientation="Vertical"> <TextBlock Text="{Binding Name}"/> <TextBlock Text="{Binding Author}"/> <TextBlock Text="{Binding Version}"/> <TextBlock Text="{Binding Description}"/> </StackPanel> </Grid> </DataTemplate> 
+2
source

Source: https://habr.com/ru/post/1333676/