WPF xaml数据触发器绑定不工作
我使用xaml和C创建了一个简单的应用程序,它将边框颜色绑定到布尔方法WPF xaml数据触发器绑定不工作,wpf,xaml,data-binding,binding,datatrigger,Wpf,Xaml,Data Binding,Binding,Datatrigger,我使用xaml和C创建了一个简单的应用程序,它将边框颜色绑定到布尔方法IsToday后面的代码。但不知怎么的,它不起作用了 有人能帮忙吗?我也尝试了INotifyPropertyChanged,但它不起作用。如果有人能帮忙,我将不胜感激,谢谢 代码隐藏: namespace WpfApplication3 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary>
IsToday
后面的代码。但不知怎么的,它不起作用了
有人能帮忙吗?我也尝试了INotifyPropertyChanged
,但它不起作用。如果有人能帮忙,我将不胜感激,谢谢
代码隐藏:
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
IsToday = true;
InitializeComponent();
}
public bool IsToday { get; set; }
}
}
命名空间WpfApplication3
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
IsToday=真;
初始化组件();
}
公共bool IsToday{get;set;}
}
}
XAML
XAML字典
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Border" x:Key="Highlight">
<Style.Triggers>
<DataTrigger Binding="{Binding IsToday}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsToday}" Value="False">
<Setter Property="Background" Value="Blue"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
您必须为
窗口设置DataContext
:
public MainWindow() {
InitializeComponent();
DataContext = this;
IsToday = true;
}
当然,这只在开始时起作用,之后对所做的每一次更改都不会起作用。正如您所知,我们必须实现INotifyPropertyChanged
您可以从xaml设置上下文
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<!-- your code -->
</Window>
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<!-- your code -->
</Window>