C# WPF - DataGrid 中的背景颜色

我是 WPF 的新手,我尝试使用自动生成的列制作 DataGrid。现在我根据需要对 DataGrid 进行了风格化,但是当 DG 小于 ParentContent 时会出现问题,它看起来像这样。

http://img1.mukewang.com/61e3be5a0001586a07970102.jpg

我不喜欢 DG 右侧那个白条的样子,但我不知道如何改变它的颜色。我希望它看起来像在 DataGridView 中。

http://img3.mukewang.com/61e3be670001cd1e07050146.jpg

我的问题是,如何更改网格那部分的颜色?



www说
浏览 413回答 2
2回答

神不在的星期二

如果您阅读 MSDN 的 DataGrid 的 ControlTemplate:你会看到大量的代码。我不知道您是否熟悉 ControlTemplates。但在此代码后面的 ScrollViewer 上有一个 ColumnDefinition :(在 MSDN 中,我的意思是)<ScrollViewer x:Name="DG_ScrollViewer" Focusable="false" Background="Black">&nbsp; &nbsp; &nbsp;<ScrollViewer.Template>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ControlTemplate TargetType="{x:Type ScrollViewer}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<Grid>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<Grid.ColumnDefinitions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<ColumnDefinition Width="Auto" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<ColumnDefinition Width="*" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<ColumnDefinition Width="Auto" />您可以看到此定义将填充父容器。因为中间列的宽度是*,它将填满整个父级。我不知道如何摆脱 WPF 默认值中的这一列。但是如果你有时间来设计你的 DataGrid。您可以更改 MSDN 的 ControlTemplate 并删除此行:&nbsp;<ColumnDefinition Width="*" />所以你会得到 DataGridView 给你的东西。

叮当猫咪

如果您阅读 MSDN 的 DataGrid 的 ControlTemplate:DataGrid 样式和模板你会看到大量的代码。我不知道您是否熟悉 ControlTemplates。但在此代码后面的 ScrollViewer 上有一个 ColumnDefinition :(在 MSDN 中,我的意思是)<ScrollViewer x:Name="DG_ScrollViewer" Focusable="false" Background="Black">&nbsp; &nbsp; &nbsp;<ScrollViewer.Template>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ControlTemplate TargetType="{x:Type ScrollViewer}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<Grid>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<Grid.ColumnDefinitions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<ColumnDefinition Width="Auto" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<ColumnDefinition Width="*" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<ColumnDefinition Width="Auto" />您可以看到此定义将填充父容器。因为中间列的宽度是*,它将填满整个父级。我不知道如何摆脱 WPF 默认值中的这一列。但是如果你有时间来设计你的 DataGrid。您可以更改 MSDN 的 ControlTemplate 并删除此行:&nbsp;<ColumnDefinition Width="*" />所以你会得到 DataGridView 给你的东西。
打开App,查看更多内容
随时随地看视频慕课网APP