WPF DataBinding不更新? [英] WPF DataBinding not updating?
本文介绍了WPF DataBinding不更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
//使用语句
命名空间NS
{
///< summary>
/// MainWindow.xaml的交互逻辑
///< / summary>
public partial class MainWindow:Window
{
private bool _test;
public bool Test
{
get {Console.WriteLine(Accessed!);返回_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 =MainWindowHeight =350Width =525 DataContext ={Binding RelativeSource = {RelativeSource Self}}>
< Grid>
< Viewbox>
< CheckBox IsChecked ={Binding Path = Test,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}/>
< / Viewbox>
< / Grid>
而且,当我设置为true时,它没有更新!
任何人都可以提出修复,或解释为什么?
谢谢,不胜感激。
解决方案
为了支持双向绑定,您的数据对象必须实施 INotifyPropertyChanged
另外,总是一个好主意,将数据从演示文稿中分离出来
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 =MainWindowHeight =350Width =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};
}
}
I have a project, where I bind a checkbox's IsChecked property with a get/set in the codebehind. However, when the application loads, it doesn't update, for some reason. Intrigued, I stripped it down to its basics, like this:
//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>
And, lo and behold, when I set it to true, it did not update!
Anyone can come up with a fix, or explain why?
Thanks, it'd be appreciated.
解决方案
In order to support two-way binding, your data object must implement INotifyPropertyChanged
Also, it's always a good idea to Separate Data from Presentation
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>
Code Behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel{Test = true};
}
}
这篇关于WPF DataBinding不更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文