5

I have a method that returns true or false.

I want this method to be binded to my DataTrigger

       <DataGrid ItemsSource="{Binding Source={StaticResource SmsData}, XPath=conv/sms}">
        <DataGrid.RowStyle>
            <Style TargetType="{x:Type  DataGridRow}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=check}" Value="true">
                        <Setter Property="Foreground" Value="Black" />
                        <Setter Property="Background" Value="Blue" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>

if returned value is "true", then do the setter...

My Code:

public MainWindow()
{
    DataContext = this;
    InitializeComponent();
}


public string check
{
    get
    {
       return "true";
    }
}

How can I get this working ? I get an error now (in runtime, not crashing my program): BindingExpression path error: 'check' property not found on 'object' ''XmlElement'

1 Answer 1

4

The DataContext of the RowStyle is an item in the ItemsSource of the DataGrid. In your case, this is an XMLElement. To Bind to the DataContext of the DataGrid, you have to refer to the DataGrid by ElementName, and the Path is the element's DataContext. Like this:

   <DataGrid Name="grid" ItemsSource="{Binding ... 
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=grid, Path=DataContext.check}" Value="true">
                    <Setter Property="Foreground" Value="Black" />
                    <Setter Property="Background" Value="Blue" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.