WPF 从代码隐藏创建样式绑定



我想在代码隐藏中创建这个 styel,但我不知道如何将绑定设置为 datagrid row 属性。

<UserControl.Resources>
<Style x:Key="MyStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="{Binding SelectedColour[0]}" />
</Style>
</UserControl.Resources>

我该怎么做? 谢谢 安德里亚

只需创建一个具有相同路径的Binding对象:

Style myStyle = new Style(typeof(DataGridCell));
myStyle.Setters.Add(new Setter(DataGridCell.BackgroundProperty, new Binding("SelectedColour[0]")));
this.Resources.Add("MyStyle", myStyle);

最新更新