I have a following XAML code (also filled with some dummy content to demonstrate the issue):
我有一个以下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>
When growing the window, it resizes correctly, but upon shrinking it refuses to do so. I have found this question which seems very much related, but I don't consider the answer in there to be a proper answer (also, I can't comment on questions/answers, otherwise, I'd write it there).
在增长窗口时,它会正确调整大小,但在缩小时它会拒绝这样做。我发现这个问题似乎非常相关,但我不认为那里的答案是正确的答案(另外,我不能评论问题/答案,否则,我会写在那里)。
The "answer" says that by default ItemsControls are grow-only, but the "by default" suggests there is a way to override this, however the answer doesn't explain how this is done and I can't find anything about it... Any ideas? I'd like to do it in XAML, it should be possible without codebehind hacks, IMO...
“答案”表示默认情况下ItemsControls仅增长,但“默认情况下”表示有一种方法可以覆盖它,但答案并不能解释这是如何完成的,我找不到任何关于它的信息。 .. 有任何想法吗?我想在XAML中做到这一点,它应该可以没有代码隐藏的黑客攻击,IMO ......
1 个解决方案
#1
9
Set ScrollViewer.HorizontalScrollBarVisibility="Disabled"
on the list box.
在列表框中设置ScrollViewer.HorizontalScrollBarVisibility =“Disabled”。
The reason is that scroll viewer (that is part of the template of ListBox
) will "prefer" to open a horizontal scroll bar than to shrink the content.
原因是滚动查看器(它是ListBox模板的一部分)将“喜欢”打开水平滚动条而不是缩小内容。
#1
9
Set ScrollViewer.HorizontalScrollBarVisibility="Disabled"
on the list box.
在列表框中设置ScrollViewer.HorizontalScrollBarVisibility =“Disabled”。
The reason is that scroll viewer (that is part of the template of ListBox
) will "prefer" to open a horizontal scroll bar than to shrink the content.
原因是滚动查看器(它是ListBox模板的一部分)将“喜欢”打开水平滚动条而不是缩小内容。