DataBinding背后的WPF代码不起作用
本文关键字:代码 不起作用 WPF 背后 DataBinding | 更新日期: 2023-09-27 18:26:24
为什么DataBinding后面的代码不工作,当我在XAML中做同样的事情时,它工作得很好。
Binding frameBinding = new Binding();
frameBinding.Source = mainWindowViewModel.PageName;
frameBinding.Converter = this; // of type IValueConverter
frameBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
frameBinding.IsAsync = true;
frame.SetBinding(Frame.ContentProperty, frameBinding);
您只设置了绑定的Source
,但没有设置其Path
。声明应该是这样的,使用mainWindowViewModel
实例作为Source
:
Binding frameBinding = new Binding();
frameBinding.Path = new PropertyPath("PageName"); // here
frameBinding.Source = mainWindowViewModel; // and here
frameBinding.Converter = this;
frameBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
frameBinding.IsAsync = true;
frame.SetBinding(Frame.ContentProperty, frameBinding);
或更短:
Binding frameBinding = new Binding
{
Path = new PropertyPath("PageName"),
Source = mainWindowViewModel,
Converter = this,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
IsAsync = true
};
frame.SetBinding(Frame.ContentProperty, frameBinding);