Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF layout: elements growing, but not shrinking correctly

I have a following XAML code (also filled with some dummy content to demonstrate the issue):

<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...

like image 823
sh code Avatar asked May 06 '13 20:05

sh code


1 Answers

Set ScrollViewer.HorizontalScrollBarVisibility="Disabled" on the list box.

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.

like image 69
XAMeLi Avatar answered Oct 15 '22 01:10

XAMeLi