Wpf 样式中带有内部边距的DataGrid初始大小错误,在调整窗口大小时会自行排序
我有一个包含许多DataGridTemplateColumns的DataGrid。其中一个有Width=“*”,其余的有Width=“Auto”。大多数只包含一个文本框。我希望每个元素的每一侧都有边距,因此在指定给文本框的样式中,我有margin=“10,1”。我观察到的是,数据网格的初始宽度太宽。如果手动使窗口变薄,DataGrid也会变薄,但会保持过宽。如果窗口变大,DataGrid在其宽度正确之前不会变宽,从那时起,调整大小将正常工作 如果我从样式中删除了边距,并将其显式地放在每列的文本框中,那么一切都会按预期进行,所以我的问题就解决了。但我还是想知道到底发生了什么,因为如果我想改变这些利润,最好是改变一种风格。这是DataGrid中的一些奇怪的bug还是我没有正确理解的东西 DataGrid嵌套在其他一些项中,但由于修复非常简单,因此我认为DataGrid和样式是唯一重要的因素 列看起来像这样Wpf 样式中带有内部边距的DataGrid初始大小错误,在调整窗口大小时会自行排序,wpf,wpf-controls,wpfdatagrid,Wpf,Wpf Controls,Wpfdatagrid,我有一个包含许多DataGridTemplateColumns的DataGrid。其中一个有Width=“*”,其余的有Width=“Auto”。大多数只包含一个文本框。我希望每个元素的每一侧都有边距,因此在指定给文本框的样式中,我有margin=“10,1”。我观察到的是,数据网格的初始宽度太宽。如果手动使窗口变薄,DataGrid也会变薄,但会保持过宽。如果窗口变大,DataGrid在其宽度正确之前不会变宽,从那时起,调整大小将正常工作 如果我从样式中删除了边距,并将其显式地放在每列的文本框
<DataGridTemplateColumn x:Name="SKUColumn"
Header="SKU"
Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="skuText"
Text="{Binding Path=SKU, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource textBodyCell}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<Style x:Key="textBodyCell" TargetType="{x:Type TextBox}">
<Setter Property="FontSize"
Value="{StaticResource bodySize}" />
<Setter Property="TextWrapping" Value="NoWrap"/>
<Setter Property="AcceptsReturn" Value="False"/>
<Setter Property="Margin" Value="10,1"/>
<Setter Property="Template" Value="{DynamicResource DataGridCellTextBox}"/>
</Style>
textBodyCell看起来像这样
<DataGridTemplateColumn x:Name="SKUColumn"
Header="SKU"
Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="skuText"
Text="{Binding Path=SKU, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource textBodyCell}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<Style x:Key="textBodyCell" TargetType="{x:Type TextBox}">
<Setter Property="FontSize"
Value="{StaticResource bodySize}" />
<Setter Property="TextWrapping" Value="NoWrap"/>
<Setter Property="AcceptsReturn" Value="False"/>
<Setter Property="Margin" Value="10,1"/>
<Setter Property="Template" Value="{DynamicResource DataGridCellTextBox}"/>
</Style>
DataGridCellTextBox所在的位置
<ControlTemplate x:Key="DataGridCellTextBox" TargetType="{x:Type TextBoxBase}">
<ScrollViewer x:Name="PART_ContentHost" Foreground="{StaticResource DataGridUnSelectedRowForeground}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}}" Value="True">
<Setter Property="Foreground" Value="{StaticResource DataGridSelectedRowForeground}" />
</DataTrigger>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}}" Value="False">
<Setter Property="Foreground" Value="{StaticResource DataGridUnSelectedRowForeground}" />
</DataTrigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>