数据触发器的DataContext设置为窗口,这就是它查找“State”属性的原因。你只需要告诉绑定器State在用户控件上即可。尝试使用以下代码:
<DataTrigger Binding="{Binding Path=State, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" Value="True">
这里是一个完整的例子:
MainWindow.xaml
<Window x:Class="WpfApplication89.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication89"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<local:UserControl1 State="{Binding Path=DualState}" />
<CheckBox Content="DualState" IsChecked="{Binding DualState}" />
</StackPanel>
</Window>
MainWindow.xaml.cs
using System.Windows;
namespace WpfApplication89
{
public partial class MainWindow : Window
{
public static readonly DependencyProperty DualStateProperty = DependencyProperty.Register("DualState", typeof(bool), typeof(MainWindow), new PropertyMetadata(false));
public bool DualState
{
get { return (bool)GetValue(DualStateProperty); }
set { SetValue(DualStateProperty, value); }
}
public MainWindow()
{
InitializeComponent();
}
}
}
UserControl1.xaml
<UserControl x:Class="WpfApplication89.UserControl1"
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:local="clr-namespace:WpfApplication89"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Text="User Control 1">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Background" Value="Beige" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=State, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" Value="true">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</UserControl>
UserControl1.xaml.cs
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication89
{
public partial class UserControl1 : UserControl
{
public static readonly DependencyProperty StateProperty = DependencyProperty.Register("State", typeof(bool), typeof(UserControl1), new PropertyMetadata(false));
public bool State
{
get { return (bool)GetValue(StateProperty); }
set { SetValue(StateProperty, value); }
}
public UserControl1()
{
InitializeComponent();
}
}
}
MainWindow.xaml.cs(实现INotifyPropertyChanged接口的版本)
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
namespace WpfApplication89
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName]string name = null)
{
if (Equals(field, value))
{
return false;
}
field = value;
this.OnPropertyChanged(name);
return true;
}
protected void OnPropertyChanged([CallerMemberName]string name = null)
{
var handler = this.PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(name));
}
#endregion
#region Property bool DualState
private bool _DualState;
public bool DualState { get { return _DualState; } set { SetProperty(ref _DualState, value); } }
#endregion
public MainWindow()
{
InitializeComponent();
}
}
}