WPF DataGridColumn Width =" 1 *"如果未设置网格宽度,有时会阻止列填充

时间:2015-07-31 14:51:38

标签: .net wpf

我在停靠面板中有两个DataGrids:

<DataGrid FontSize="16" Margin="5" CellStyle="{StaticResource DataGrid_CellStyle_CentreAlign}" RowHeight="40" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserResizeColumns="False" ItemsSource="{Binding Products}" AutoGenerateColumns="False">
  <DataGrid.Columns>
         <DataGridTextColumn Header="Product" Binding="{Binding Product}"/>
         <DataGridTextColumn Header="Description" Binding="{Binding ProductDescription}" Width="1*"/>
         <DataGridTextColumn Header="Qty"  CellStyle="{StaticResource DataGrid_CellStyle_CentreRightAlign}">
               <DataGridTextColumn.Binding>
                      <MultiBinding  StringFormat="{}{0:N0} {1}">
                           <Binding Path="Qty"/>
                           <Binding Path="QtyUOM"/>
                       </MultiBinding>
                </DataGridTextColumn.Binding>
          </DataGridTextColumn>
          <DataGridTextColumn Header="Weight" CellStyle="{StaticResource DataGrid_CellStyle_CentreRightAlign}">
                 <DataGridTextColumn.Binding>
                       <MultiBinding   StringFormat="{}{0:N2} {1}">
                             <Binding Path="Weight"/>
                             <Binding Path="WeightUOM"/>
                       </MultiBinding>
                 </DataGridTextColumn.Binding>
          </DataGridTextColumn>
    </DataGrid.Columns> 
</DataGrid>

第二个:

 <DataGrid FontSize="16" Margin="5" HorizontalAlignment="Stretch" CellStyle="{StaticResource DataGrid_CellStyle_CentreAlign}" RowHeight="40" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserResizeColumns="False" ItemsSource="{Binding Pallets}" AutoGenerateColumns="False">
  <DataGrid.Columns>
        <DataGridTextColumn Header="Pallet ID"  Binding="{Binding PalletId}"  />
        <DataGridTextColumn Header="Product" Binding="{Binding Product}" Width="1*" />

   </DataGrid.Columns>
 </DataGrid>

正如您所看到的,它们非常相似并且在设计器中正确显示,但运行时结果如下:

Grids

如您所见,第二个网格上的列大小错误,描述列应扩展为填充,托盘列应自动调整大小。

到目前为止,我所做的只是与DataGrid的宽度有关,因为通过设置列正确显示的第二个DataGrid的宽度(但网格本身的大小不再是动态的)。

1 个答案:

答案 0 :(得分:0)

当您将Windows WindowState设置为Normal并在Loaded事件中设置窗口的Width和Height时,如果在动态宽度Grid列中有DataGrid,则会发生这种情况。在构造函数中设置它可以解决问题。