Есть элемент, используемый в шаблоне DataGrid, со свойством IsSelected, привязанным к одноименному свойству объекта.
//Расширенный Лэйбл
//XAML
<Label x:Class="Solution.XLabel"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
x:Name="Parent">
</Label>
//code behind
partial class XLabel
{
public XLabel()
{
InitializeComponent();
}
public bool IsSelected
{
get { return (bool) GetValue(IsSelectedProperty); }
set { SetValue(IsSelectedProperty, value);}
}
public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register(
"IsSelected", typeof (bool), typeof (XLabel), new FrameworkPropertyMetadata(default(bool)));
}
//XAML
<DataTemplate x:Key="CellTemplate" DataType="data:DataRowView">
<local:XLabel x:Name="Label" Content="{Binding Path=Title}"
Background="{Binding Path=BackColor}"
IsSelected="{Binding Path=IsSelected}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type DataGridCell}},
Path=IsSelected}" Value="True">
<Setter TargetName="Label" Property="IsSelected" Value="True" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
//Объект привязки
class Data : INotifyPropertyChanged
{
private bool _isSelected;
public bool IsSelected
{
get { return this._isSelected; }
set
{
this._isSelected = value; //при срабатывании триггера не меняется
_OnPropertyChanged("IsSelected");
}
}
}
Если менять свойство элемента через триггер, то привязка к объекту не срабатывает, т.е. свойство объекта не изменяется. Как заставить работать привязку в таком случае?
XLabel
определено именно так?