2016-06-09 56 views
1

我有一個XAML用戶控件如下:WPF DataTrigger不與視圖模型物業工作

<UserControl x:Class="Presentation.Views.PersonListView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro.Platform" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300" 
     MinWidth="300" MinHeight="300"> 
<Grid> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="10"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <TextBlock Grid.Row="0" Text="List of Persons"/> 
     <DataGrid SelectionMode="Single" SelectionUnit="FullRow" Grid.Row="2" x:Name="Persons" IsReadOnly="True" 
        cal:Message.Attach="[Event MouseDoubleClick] = [Action OpenPersonDetail()]" SelectedItem="{Binding SelectedPerson}"> 
      <DataGrid.RowStyle> 
       <Style TargetType="DataGridRow"> 
        <Setter Property="Background" Value="White"/> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding DataRowChanged}" Value="True"> 
          <Setter Property="Background" Value="Red"/> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </DataGrid.RowStyle> 
     </DataGrid> 
    </Grid> 
</Grid> 

而且ViewModel類:

public class PersonListViewModel 
{ 
    ... 

    private bool m_DataRowChanged; 

    public IObservableCollection<Person> Persons { 
     get; 
     set; 
    } 

    public bool DataRowChanged 
    { 
     get { return m_DataRowChanged; } 
     set 
     { 
      m_DataRowChanged = value; 
      NotifyOfPropertyChange(() => DataRowChanged); 
     } 
    } 

    public void Handle(Person p) 
    { 
     GetPersonList(); 
     foreach (var item in Persons) 
     { 
      if (!item.Version.Equals(p.Version)) 
      { 
       DataRowChanged = true; 
       break; 
      } 
     } 
    } 
} 

Person是其中包含了一些類屬性:名字,姓氏,年齡...

但它不起作用ause DataRowChanged不是Person的成員。如果我通過XAML中的Person的財產之一更改DataRowChanged,它將起作用。 任何人都可以幫助我。提前致謝。

回答

3

試試這個:

<DataTrigger Binding="{Binding DataContext.DataRowChanged, 
         RelativeSource={RelativeSource AncestorType=UserControl}}" 
      Value="True"> 
    <Setter Property="Background" Value="Red"/> 
</DataTrigger> 
+0

哦,你救了我的一天。 –

+2

@QuanNguyen我希望你也從中學到了一些東西,不僅僅是複製到你的項目中,而且很高興它能夠工作。瞭解這種工作原理將有助於加深對整個綁定引擎的理解,並使您將來的綁定體驗更好。 – Jai