Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odd WPF Grid Behavior Affected by TextBox Text

Tags:

wpf

I can't figure out why the contents of my TextBox is affecting my grid column widths. I have setup a grid with 3 columns with widths defined as 50, *, 50, as shown below

Grid Setup

Now, when in use, the center column will grow/shrink as the text in the TextBox changes, see the 2 examples below. The actual TextBox is not changing size, so I can't understand why in the world the grid is changing. The Grid is inside of a Border inside a UserControl. The UserControl is used as a DataTemplate in a ListBox.

Initial Grid with No input in the 1st TextBox

Grid Layout with long string in 1st TextBox

Edit: I've discovered that this issue is related to the UserControl being in a ListBox, see example image below (UserControl in ListBox (circled in red) vs. UserControl Placed on Form (circed in blue). The grid behaves as expected when the UserControl is placed directly on the form. Code for the form is given below.

UserControl in ListBox vs. UserControl Placed on Form

UserControl XAML:

<Grid ShowGridLines="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50"/>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="50"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBlock Text="Name:" Margin="2" />
        <TextBox Grid.Column="1" Margin="2" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Grid.ColumnSpan="2" />

        <TextBlock Text="Shift:" Grid.Row="1" Margin="2"  />
        <TextBox  Grid.Column="1" Grid.Row="1" Margin="2" Text="{Binding TimeShift, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, FallbackValue=0}" HorizontalContentAlignment="Right" />
        <TextBlock Text="s" Grid.Row="1" Grid.Column="2" Margin="2" />
    </Grid>

Window/Form XAML:

<Window x:Class="CrashSimOffice.FileImport"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CrashSimOffice"
    Title="File Import" Height="350" Width="450" Background="White" Icon="/CrashSimOffice;component/Images/16/page-white-save.png">
<Grid>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="75"/>
        <ColumnDefinition Width="75"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="75"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Button Content="Add" Margin="2" Command="{Binding AddFileCommand}" />
    <Button Content="Remove" Grid.Column="1" Margin="2" Command="{Binding RemoveFileCommand}" />

    <ListBox HorizontalContentAlignment="Stretch" Grid.ColumnSpan="4" Margin="2" Grid.Row="1" ItemsSource="{Binding Files}" SelectedItem="{Binding CurrentFile}"  ScrollViewer.CanContentScroll="False" Background="WhiteSmoke">
        <ListBox.ItemTemplate>
            <DataTemplate DataType="{x:Type local:FileViewModel}">
                <local:FileView DataContext="{Binding}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <Button Content="Done" Grid.Column="3" Margin="2" Grid.Row="2" Click="Button_Click" />


    <local:FileView Grid.Row="3" Grid.ColumnSpan="4" />
</Grid>

like image 447
bradcarman Avatar asked Nov 24 '25 20:11

bradcarman


1 Answers

OK, I figured it out, Bruno V you got me thinking it must have something to do with the ListBox. I needed to add this to my ListBox:

ScrollViewer.HorizontalScrollBarVisibility="Disabled"

Not sure why that works, but it does.

like image 125
bradcarman Avatar answered Nov 26 '25 17:11

bradcarman