WPF从后面的代码创建样式绑定

时间:2017-07-10 12:30:15

标签: c# wpf binding styles code-behind

我想在codebehind中创建这个样式,但我不知道如何设置绑定到datagrid行属性。

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

我该怎么办? 谢谢 安德莉亚

1 个答案:

答案 0 :(得分:3)

只需使用相同的路径创建Binding对象:

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