WPF DataTrigger 不适用于 ViewModel 属性
WPF DataTrigger doesn't work with ViewModel property
我在 XAML 中有一个用户控件,如下所示:
<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 class:
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
是一个 class,其中包含一些属性:FirstName、LastName、Age...
但它不起作用,因为 DataRowChanged
不是 Person
的成员。如果我将 DataRowChanged
更改为 XAML 中 Person
的 属性 之一,它将起作用。
任何人都可以帮助我。提前致谢。
试试这个:
<DataTrigger Binding="{Binding DataContext.DataRowChanged,
RelativeSource={RelativeSource AncestorType=UserControl}}"
Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
我在 XAML 中有一个用户控件,如下所示:
<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 class:
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
是一个 class,其中包含一些属性:FirstName、LastName、Age...
但它不起作用,因为 DataRowChanged
不是 Person
的成员。如果我将 DataRowChanged
更改为 XAML 中 Person
的 属性 之一,它将起作用。
任何人都可以帮助我。提前致谢。
试试这个:
<DataTrigger Binding="{Binding DataContext.DataRowChanged,
RelativeSource={RelativeSource AncestorType=UserControl}}"
Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>