wpf 数据触发器绑定到方法

ken*_*eno 5 c# wpf bind datatrigger

我有一个返回 true 或 false 的方法。

我希望将此方法绑定到我的 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>
Run Code Online (Sandbox Code Playgroud)

如果返回值为“true”,则执行设置器...

我的代码:

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


public string check
{
    get
    {
       return "true";
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能让它工作?我现在收到错误(在运行时,不会使程序崩溃): BindingExpression 路径错误:在“对象”“XmlElement”上找不到“检查”属性

And*_*law 4

RowStyle 的DataContext 是DataGrid 的ItemsSource 中的一项。在您的例子中,这是一个 XMLElement。要绑定到DataGrid的DataContext,您必须通过ElementName引用DataGrid,并且Path是元素的DataContext。像这样:

   <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>
Run Code Online (Sandbox Code Playgroud)