Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在WPF中设置DataGridRow颜色和交替行颜色?_Wpf_Templates_Binding_Datagrid_Datagridrow - Fatal编程技术网

如何在WPF中设置DataGridRow颜色和交替行颜色?

如何在WPF中设置DataGridRow颜色和交替行颜色?,wpf,templates,binding,datagrid,datagridrow,Wpf,Templates,Binding,Datagrid,Datagridrow,如何将DataGridRow颜色设置为DataGrid依赖属性RowBackground <Style TargetType="{x:Type DataGrid}" x:Key="EmployeeDataGridStyle"> <Setter Property="RowBackground" Value="White"/> <Setter Property="Alte

如何将
DataGridRow
颜色设置为
DataGrid
依赖属性
RowBackground

<Style TargetType="{x:Type DataGrid}" x:Key="EmployeeDataGridStyle">

  <Setter Property="RowBackground" Value="White"/>
  <Setter Property="AlternatingRowBackground" Value="LightCyan"/>
  ...
</Style>

...

...

可以使用
相对资源
绑定来绑定到
DataGrid
RowBackground

Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=RowBackground}"
但是,
To
属性上的
coloranamition
中的第二个绑定是不可能的

不能使用动态资源引用或数据绑定表达式设置情节提要或动画属性值。这是因为ControlTemplate中的所有内容都必须是线程安全的,计时系统必须冻结Storyboard对象以使其具有线程安全性。如果情节提要或其子时间线包含动态资源引用或数据绑定表达式,则无法冻结该情节提要。有关冻结和其他可冻结功能的详细信息,请参阅

您可以使用与上述相同的方式将动画绑定到不同的
AlternatingRowBackground

Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=AlternatingRowBackground}"

DataGridRow具有AlternationIndex属性。它可以绑定到AlternationConverter中指定的颜色集合。示例您还可以使用ItemsControl.AlternationIndex Attachable属性。示例为什么要使用VisualStateManager?使用触发器更容易做到这一点。您有什么类型的项目WPF框架、WPF核心、UWP?@EldHasp我正在使用WPF核心框架,我听说VisualStateManager为您提供了更好的灵活性和更多的选项,而不是触发器,因此我选择使用它。上面两个链接中的方法将不允许我对datagrid dependency属性权限中设置的值进行模板设置?使用此模板您试图实现什么?默认值处理交替计数和背景颜色,而不改变。你看了吗?如果你在修改模板,在我看来,最好是从原始模板开始,然后用小的修改进行迭代,检查它是否仍然有效,小的修改。。。等等直到你得到你想要的。一次完成的每件事通常都会导致完全无法挽回的损坏。这将设置datagridrow模板border.background的background属性,对吗?如何设置它,使其引用datagrid中定义的Rowbackground和AlternatingRowBackground属性?@JeffreyChen修改了我的答案,现在我理解了您的意图。
Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=AlternatingRowBackground}"