Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF create style binding from code behind

I would like to create this styel in codebehind, but I don't know how to set the binding to the datagrid row property.

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

How can I do it? Thank you Andrea

like image 359
andrea Avatar asked Feb 14 '26 20:02

andrea


1 Answers

Just create a Binding object with the same path:

Style myStyle = new Style(typeof(DataGridCell));
myStyle.Setters.Add(new Setter(DataGridCell.BackgroundProperty, new Binding("SelectedColour[0]")));
this.Resources.Add("MyStyle", myStyle);
like image 119
mm8 Avatar answered Feb 17 '26 10:02

mm8



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!