How to set 50% of my monitor width in a grid?

Viewed 81

I want to set width 50% from my monitor.

The first line should be the same as the second:

The first line should be the same as the second

My code:

<DataTemplate x:Key="RowTemplate">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBox Grid.Column="0"
                 Grid.Row="1"
                 TextWrapping="Wrap"
                     Text="{Binding Mask}"/>
        <TextBox Grid.Column="1"
                 Grid.Row="1"
                 TextWrapping="Wrap"
                     Text="{Binding Value}"/>
    </Grid>
</DataTemplate>

It should be like that

1 Answers

I reproduced your issue using a listbox.

The behaviour is because there's a scrollviewer in a listbox and that tells the content it can have as much width as it likes ( and height ). Hence there's no set amount for the * measure to be half of.

You can avoid this behaviour by disabling the horizontal scroll. I had to also force the row to stretch to fit it's parent.

Working markup:

<Window.Resources>
    <DataTemplate x:Key="RowTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBox Grid.Column="0"
             TextWrapping="Wrap"
                 Text="{Binding Mask}"/>
            <TextBox Grid.Column="1"
             TextWrapping="Wrap"
                 Text="{Binding Value}"/>
        </Grid>
    </DataTemplate>
</Window.Resources>
<Grid>
    <ListBox ItemTemplate="{StaticResource RowTemplate}"
             ItemsSource="{Binding MyRows}"
             HorizontalContentAlignment="Stretch"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
    </ListBox>
</Grid>
</Window>

The two key lines here are:

             HorizontalContentAlignment="Stretch"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled" >

With some test data I see:

enter image description here

Related