Cannot resolve binding in datatrigger wpf

597 views Asked by At

Using this code:

  <DataGrid
    Grid.Row="1"
    ItemsSource="{Binding DdaOptions.FilteredRecords.FilteredRecords}"
    AutoGenerateColumns="False"
    VerticalScrollBarVisibility="Auto"
    GridLinesVisibility="None"
    Background="{StaticResource White1}"
    BorderThickness="1"
    BorderBrush="{StaticResource White1}"
    Style="{StaticResource Roboto10DataGrid}"
    AreRowDetailsFrozen="True"
    CanUserAddRows="False">
  <DataGrid.Columns>
    <DataGridTextColumn Header="GuideNumber" Binding="{Binding GuideNumber}" />
    <DataGridTextColumn Header="PartNumber" Binding="{Binding PartNumber}" />
    <DataGridTextColumn Header="Options" Binding="{Binding Options}" />
    <DataGridTextColumn Header="Description" Binding="{Binding Description}" />
    <DataGridTextColumn Header="PartStock" Binding="{Binding PartStock}" />
    <DataGridTextColumn Header="InterventionType" Binding="{Binding InterventionType}" />
  </DataGrid.Columns>
<DataGrid.RowStyle>
    <Style TargetType="DataGridRow" BasedOn="{StaticResource {x:Type DataGridRow}}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding IsInDda}" Value="True">
          <Setter Property="Foreground" Value="{StaticResource DarkAccent1}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Highlighted}" Value="True">
          <Setter Property="Foreground">
            <Setter.Value>
             <SolidColorBrush Color="{Binding DdaOptions.MyColor}"/>
            </Setter.Value>
          </Setter>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </DataGrid.RowStyle>
</DataGrid>

the binding Color="{Binding DdaOptions.MyColor}" does not work. If I replace the line

<SolidColorBrush Color="{Binding DdaOptions.MyColor}"/>

with this line

<SolidColorBrush Color="Red"/>

it works. Using debug the DdaOptions.MyColor property is properly set. How can I change the code in order to solve this problem? I've tried to use a converter instead of

<SolidColorBrush Color="{Binding DdaOptions.MyColor}"/>

but the problem still remains. The type of the property MyColor is Color. Dda is the ViewModel and it implements INotifyPropertyChanged. Any idea?

UPDATE

Using this code

 <DataTrigger Binding="{Binding Highlighted}" Value="True">
   <Setter Property="Foreground" Value="{Binding DdaOptions.MyColor}"/>
 </DataTrigger>

instead of

  <DataTrigger Binding="{Binding Highlighted}" Value="True">
    <Setter Property="Foreground">
      <Setter.Value>
        <SolidColorBrush Color="{Binding DdaOptions.MyColor}"/>
      </Setter.Value>
    </Setter>
  </DataTrigger>

The color change always as black. MyColor is a Brush and I set it in this way:

MyColor = new SolidColorBrush(Color.FromRgb(myColor.R, myColor.G, yColor.B));
2

There are 2 answers

0
Nawed Nabi Zada On BEST ANSWER

First of all:

if DdaOptions.MyColor is of type SolidColorBrush then use this:

<DataTrigger Binding="{Binding Highlighted}" Value="True">
    <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=DataContext.DdaOptions.MyColor}"/>
</DataTrigger>

or if of type Color then this:

<DataTrigger Binding="{Binding Highlighted}" Value="True">
  <Setter Property="Foreground">
    <Setter.Value>
      <SolidColorBrush Color="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=DataContext.DdaOptions.MyColor}"/>
    </Setter.Value>
  </Setter>
</DataTrigger>

And of course your have to change AncestorType=Window to the correct type.

4
mm8 On

If the DdaOptions.MyColor source property returns a Brush you should set the Foreground property to this brush directly:

<DataTrigger Binding="{Binding Highlighted}" Value="True">
    <Setter Property="Foreground" Value="{Binding DdaOptions.MyColor}" />
</DataTrigger>