【问题标题】:wpf datatrigger bind to methodwpf datatrigger绑定到方法
【发布时间】:2012-12-06 23:46:10
【问题描述】:

我有一个返回 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>

如果返回值为“true”,则执行setter...

我的代码:

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


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

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

【问题讨论】:

    标签: c# wpf bind datatrigger


    【解决方案1】:

    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>
    

    【讨论】:

      猜你喜欢
      • 2012-01-09
      • 2017-02-16
      • 2013-06-04
      • 2014-05-28
      • 2013-07-10
      • 2011-08-16
      • 1970-01-01
      • 2014-09-09
      • 2011-09-15
      相关资源
      最近更新 更多