C# WPF数据绑定未更新?

C# WPF数据绑定未更新?,c#,wpf,data-binding,C#,Wpf,Data Binding,我有一个项目,在那里我将复选框的IsChecked属性与代码隐藏中的get/set绑定。但是,当应用程序加载时,由于某些原因,它不会更新。出于好奇,我将其简化为基本内容,如下所示: //using statements namespace NS { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainW

我有一个项目,在那里我将复选框的IsChecked属性与代码隐藏中的get/set绑定。但是,当应用程序加载时,由于某些原因,它不会更新。出于好奇,我将其简化为基本内容,如下所示:

//using statements
namespace NS
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool _test;
        public bool Test
        {
            get { Console.WriteLine("Accessed!"); return _test; }
            set { Console.WriteLine("Changed!"); _test = value; }
        }
        public MainWindow()
        {
            InitializeComponent();
            Test = true;
        }
    }
}
//使用语句
名称空间NS
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
私人布尔检验;
公共布尔测试
{
获取{Console.WriteLine(“Accessed!”);返回_test;}
设置{Console.WriteLine(“Changed!”);_test=value;}
}
公共主窗口()
{
初始化组件();
测试=真;
}
}
}
XAML:


而且,瞧,当我把它设置为真时,它并没有更新

任何人都可以想出解决办法,或者解释原因


非常感谢。

为了支持数据绑定,您的数据对象必须实现

而且,这总是一个好主意


我不认为我应该被否决,因为我读了另一个来源,而不是MSDN…@我没有否决,但我怀疑这是因为这个问题没有显示任何研究成果。这个问题非常基本,任何使用WPF绑定系统的人都知道,您需要实现
INotifyPropertyChanged
,使您的属性在发生更改时通知UI重新评估绑定。几乎每一个介绍绑定的WPF教程都包含了这个概念,这就像说新手应该被否决,因为他们不知道如何使用基本方法。这是缺乏知识。WPF对我来说是个新鲜事物。@ofstream的否决票并不是因为你缺乏知识,而是因为你缺乏研究努力。你也可以说,新手应该因为缺乏研究努力而被否决。MSDN+MSDN博客可能很容易回答这里发布的所有问题中约25%的问题。
<Window x:Class="TheTestingProject_WPF_.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}}">
<Grid>
    <Viewbox>
        <CheckBox IsChecked="{Binding Path=Test, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    </Viewbox>
</Grid>
public class ViewModel: INotifyPropertyChanged
{
    private bool _test;
    public bool Test
    {  get { return _test; }
       set
       {
           _test = value;
           NotifyPropertyChanged("Test");
       }
    }

    public PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyName)
    {
         if (PropertyChanged != null)
             PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

<Window x:Class="TheTestingProject_WPF_.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">
<Grid>
    <Viewbox>
        <CheckBox IsChecked="{Binding Path=Test, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    </Viewbox>
</Grid>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel{Test = true};
    }
}