我正在尝试使用WPF中的listBox呈现活动源类型的UI。在调整窗口大小时,listBox可以完美呈现并调整自身大小。但是,内部物品没有按原样包裹其内容。因此,我将内部Grid的宽度设置为listBox的ActualWidth。这是我的XAML和一些截图,用于解释我想要实现的目标。
<Grid x:Class="MyAddin.WPFControls"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:c="clr-namespace:MyAddin"
mc:Ignorable="d" Background="Transparent"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ShowGridLines="False" d:DesignHeight="221" d:DesignWidth="275">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock HorizontalAlignment="Stretch"
IsManipulationEnabled="True" Grid.Row="0"
Height="20" Grid.ColumnSpan="5" TextAlignment="Right" Padding="0,0,5,0" Margin="0,5,0,0" FontSize="14" FontStyle="Normal" FontWeight="ExtraLight">Activity Feed</TextBlock>
<ListBox Grid.Row="1" Name="listBox1" IsSynchronizedWithCurrentItem="True"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
VerticalContentAlignment="Stretch" ItemContainerStyle="{StaticResource ContainerStyle}"
AlternationCount="2" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid MaxWidth="{Binding ElementName=listBox1, Path=ActualWidth}" Height="40">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="34"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0">
<Image Source="{Binding Path=AvatarURL}" Stretch="Fill" Width="32" Height="32" />
</Border>
<StackPanel Grid.Column="1">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=FirstName }" TextBlock.FontWeight="Bold"
HorizontalAlignment="Left" />
<TextBlock><Bold> - </Bold></TextBlock>
<TextBlock Text="{Binding Path=TS}" TextWrapping="Wrap"
TextTrimming="WordEllipsis" FontSize="9" Margin="0,2,0,0" FontWeight="ExtraLight" />
</StackPanel>
<TextBlock Text="{Binding Path=Comment}" TextWrapping="Wrap"
TextTrimming="CharacterEllipsis" HorizontalAlignment="Stretch"
VerticalAlignment="Top" Padding="0,0,5,0"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
并将其呈现为图像1.如果我重新调整窗口大小,列表框内容将按照我的预期进行包装。然而,当我重新调整窗口大小时,布局被打破,如图3所示。我需要帮助解决2个问题:
答案 0 :(得分:3)
应该禁用listBox上的属性ScrollViewer.HorizontalScrollBarVisibility而不是隐藏。