我有以下XAML代码:
<ListView Background="Blue" x:Name="lstFriends" Grid.Row="2" HorizontalContentAlignment="Stretch" >
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid Background="Pink">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="64" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="ms-appx:///Assets/Logo.png" Margin="12" VerticalAlignment="Top"></Image>
<StackPanel Orientation="Vertical" Grid.Column="1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Path='Fullname'}" Style="{StaticResource ListViewItemSubheaderTextBlockStyle}"></TextBlock>
<Image Grid.Column="1" Source="{Binding Path='OnlineIcon'}" Width="16" Height="16" HorizontalAlignment="Right" />
</Grid>
<TextBlock Text="{Binding Path='Subtitle'}" Style="{StaticResource ListViewItemContentTextBlockStyle}" TextWrapping="WrapWholeWords" Margin="12"></TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我已经在这2篇文章中设置了HorizontalContentAlignment
和ItemContainerStyle
:
How to set width to 100% in WPF
XAML Columndefinitions width * not taking available space
然而,它仍然不起作用。网格只占用TextBlock
和Image
所需的空间。
我做错了什么?如何让网格占用所有空间,将OnlineIcon带到右边?
答案 0 :(得分:4)
添加:
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<ListView.ItemContainerStyle>
中的应该可以胜任。
你在问题中提到了 HorizontalContentAlignment ,但是我看到你没有在xaml代码中设置它。默认情况下, HorizontalContentAlignment 设置为 Left ,因此不占用所有可用空间。