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

WPF create style binding from code behind

我想在代码隐藏中创建这种样式,但我不知道如何设置绑定到数据网格行 属性。

 <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);