Width problem with * (star) in WPF Grid - ColumnDefinition

616 views Asked by At

I have tried ColumnDefinition Width="5*" and also with 3*, 1* to ColumnDefinition which aplied to Grid container and the parent control is ListBox but it does not reflect the expected width. refer XAML code as below

 <StackPanel Height="Auto" HorizontalAlignment="Left">
     <ListBox x:Name="myListView2" BorderThickness="0" Background="#635F5F">
    <ListBox.ItemsPanel>
     <ItemsPanelTemplate>
     <StackPanel Background="#635F5F" HorizontalAlignment="Left" VerticalAlignment="Stretch" Orientation="Vertical" />
     </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
     <DataTemplate>
      <Border BorderThickness="10" BorderBrush="#413e3e" Background="#413e3e" Margin="5, 5, 5, 5" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center">
    <Grid>
     <Grid.ColumnDefinitions>
      <ColumnDefinition Width="5*"></ColumnDefinition>
      <ColumnDefinition Width="3*"></ColumnDefinition>
      <ColumnDefinition Width="1*"></ColumnDefinition>
     </Grid.ColumnDefinitions>
     <Grid.RowDefinitions>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
     </Grid.RowDefinitions>
    
     <TextBlock Style="{StaticResource GridRows}" Grid.Row="0" Grid.Column="0" Text="{Binding OrderNumber}" ></TextBlock>
     <TextBlock Style="{StaticResource GridRows}" Grid.Row="0" Grid.Column="1" Text="{Binding LABEL}" ></TextBlock>
     <TextBlock Style="{StaticResource GridRows}" Grid.Row="0" Grid.Column="2" Text="{Binding Amount}" ></TextBlock>
    
     <TextBlock Style="{StaticResource GridRows}" Grid.Row="1" Grid.Column="0" Text="{Binding Size}" Margin="0, 5, 0, 0"></TextBlock>
     <Border BorderThickness="1" Height="25" Width="30" BorderBrush="red" Background="Red" CornerRadius="10" Grid.Row="1"  Grid.Column="1"  HorizontalAlignment="Left">
      <TextBlock FontSize="9" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding LabelRadius}" />
     </Border>
     <TextBlock Style="{StaticResource GridRows}" Grid.Row="1" Grid.Column="2" Text="{Binding AmountVal1}" Margin="0, 5, 0, 0" ></TextBlock>
    </Grid>
      </Border>
     </DataTemplate>
    </ListBox.ItemTemplate>
      </ListBox>
     </StackPanel>
     </StackPanel>
1

There are 1 answers

0
Florian On

It might work if you set the Grid width:

<Grid Width="300">
 <Grid.ColumnDefinitions>
  <ColumnDefinition Width="5*"></ColumnDefinition>
  <ColumnDefinition Width="3*"></ColumnDefinition>
  <ColumnDefinition Width="1*"></ColumnDefinition>
 </Grid.ColumnDefinitions>
 <Grid.RowDefinitions>
  <RowDefinition></RowDefinition>
  <RowDefinition></RowDefinition>
  <RowDefinition></RowDefinition>
  <RowDefinition></RowDefinition>
 </Grid.RowDefinitions>

 <TextBlock Style="{StaticResource GridRows}" Grid.Row="0" Grid.Column="0" Text="{Binding OrderNumber}" ></TextBlock>
 <TextBlock Style="{StaticResource GridRows}" Grid.Row="0" Grid.Column="1" Text="{Binding LABEL}" ></TextBlock>
 <TextBlock Style="{StaticResource GridRows}" Grid.Row="0" Grid.Column="2" Text="{Binding Amount}" ></TextBlock>

 <TextBlock Style="{StaticResource GridRows}" Grid.Row="1" Grid.Column="0" Text="{Binding Size}" Margin="0, 5, 0, 0"></TextBlock>
 <Border BorderThickness="1" Height="25" Width="30" BorderBrush="red" Background="Red" CornerRadius="10" Grid.Row="1"  Grid.Column="1"  HorizontalAlignment="Left">
  <TextBlock FontSize="9" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding LabelRadius}" />
 </Border>
 <TextBlock Style="{StaticResource GridRows}" Grid.Row="1" Grid.Column="2" Text="{Binding AmountVal1}" Margin="0, 5, 0, 0" ></TextBlock>