我有以下 XAML 代码(也填充了一些虚拟内容来演示该问题):
<Window x:Class="WpfWatchBird.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="378" Width="728"
Name="frmMainList">
<Grid Name="grdMainLayout">
<ListBox Name="lstData" SelectionChanged="lstData_SelectionChanged" HorizontalAlignment="Stretch">
<ListBoxItem HorizontalContentAlignment="Stretch">
<ListBoxItem.Content>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="100"/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<Label Content="nick" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Teal" Grid.Column="0" Grid.Row="0" Margin="0" Padding="0"/>
<Label Content="kategória" VerticalAlignment="Center" Foreground="Green" Grid.Column="1" Grid.Row="0" Margin="10,0,0,0" Padding="0"/>
<Label Content="stav záznamu" Foreground="Gray" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0" Padding="0" Grid.Column="2" Grid.Row="0"/>
<Label Content="1.1. 2013 12:00" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,5,0" Padding="0" Grid.Column="3" Grid.Row="0"/>
<Image HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="0" Grid.Row="1" Source="http://www.birdz.sk/la/bezfotky.gif" />
<StackPanel x:Name="spItemText" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="3" HorizontalAlignment="Stretch">
<TextBlock MaxWidth="{Binding ElementName=spItemText,Path=ActualWidth}" FontSize="18" TextWrapping="Wrap">
<TextBlock.Text>Nadpis blogu, konečne, dámy a páni :)</TextBlock.Text>
</TextBlock>
<TextBlock HorizontalAlignment="Stretch" xml:space="preserve" TextWrapping="Wrap" Margin="10,0,0,0"
Padding="0" VerticalAlignment="Top"
MaxWidth="{Binding ElementName=spItemText, Path=ActualWidth}">
<TextBlock.Text>Preview textu blogu, blablabla, blabla, bude to pekné a krásne
a bude to vedieť robiť aj newliny, len ešte nejak spraviť zarovnanie, aby nebolo... aha, ono nie je, on mi len kvôli xml:space preserve... A už mi aj krásne funguje textwrapping, keď som poodstraňoval zopár nepotrebných
...hmm... tak jeden riadok to wraplo dobre, ale ten ďalší už nie, zaujímavé...
</TextBlock.Text>
</TextBlock>
</StackPanel>
<Label Grid.Column="1" Grid.Row="2">
<Hyperlink>Otvor originál</Hyperlink>
</Label>
<Label Grid.Column="3" Grid.Row="2" HorizontalAlignment="Right">
<Hyperlink>Otvor zložku so zálohou</Hyperlink>
</Label>
</Grid>
</ListBoxItem.Content>
</ListBoxItem>
<ListBoxItem Content="Menu 2" />
</ListBox>
</Grid>
</Window>
扩大窗口时,它会正确调整大小,但在缩小时它拒绝这样做。我发现这个问题似乎非常相关,但我不认为那里的答案是正确的答案(另外,我不能评论问题/答案,否则,我会写在那里)。
“答案”说默认情况下 ItemsControls 是仅增长的,但“默认情况下”表明有一种方法可以覆盖 this,但是答案没有解释这是如何完成的,我找不到任何关于它的信息。 .. 有任何想法吗?我想在 XAML 中做到这一点,它应该可以在没有代码隐藏黑客的情况下,IMO ......