WPF布局:元素在增长,但没有正确缩小

时间:2013-05-06 20:24:48

标签: wpf xaml resize autoresize

我有一个以下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>

在增长窗口时,它会正确调整大小,但在缩小时它会拒绝这样做。我找到this question which seems very much related,但我不认为那里的答案是正确的答案(另外,我不能评论问题/答案,否则,我会在那里写。)

“答案”表示默认情况下ItemsControls仅限增长,但“默认情况下”表示有一种方法可以覆盖此,但答案并未解释如何完成此操作我找不到任何关于它的东西......有什么想法吗?我想在XAML中做到这一点,应该可以在没有代码隐藏的情况下实现,IMO ......

1 个答案:

答案 0 :(得分:9)

在列表框中设置ScrollViewer.HorizontalScrollBarVisibility="Disabled"

原因是滚动查看器(它是ListBox模板的一部分)将“喜欢”打开水平滚动条而不是缩小内容。