我有一个项目,在其中将复选框的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;
}
}
}
XAML:
<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>
神奇的是,当我将其设置为true时,它并没有更新!
有人能想出一个解决办法或解释一下吗?
谢谢,感激不尽。
INotifyPropertyChanged
来使属性在改变时通知UI重新评估绑定。几乎每一个介绍WPF绑定的教程都涵盖了这个概念。 - Rachel