2012-12-19 104 views
4

我有一个返回true或false的方法。wpf datatrigger绑定到方法

我想这个方法被绑定到我的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> 

如果返回值是 “真”,然后做二传手...

我的代码:

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


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

我怎样才能得到这个工作?我现在得到一个错误(在运行时,不会崩溃我的程序): BindingExpression路径错误:“检查”财产“对象”“”的XmlElement”上没有找到

回答

3

的RowStyle的DataContext的是在的的ItemsSource的项目DataGrid。就你而言,这是一个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>