C# WPF代码隐藏数据绑定不工作

C# WPF代码隐藏数据绑定不工作,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,为什么数据绑定背后的代码不起作用,当我在XAML中做同样的事情时,它工作得很好 Binding frameBinding = new Binding(); frameBinding.Source = mainWindowViewModel.PageName; frameBinding.Converter = this; // of type IValueConverter frameBinding.UpdateSourceTrigger = UpdateSourceTrigger.Pro

为什么数据绑定背后的代码不起作用,当我在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);

您只设置了绑定的
,而没有设置其
路径
。声明应如下所示,使用
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);

您只设置了绑定的
,而没有设置其
路径
。声明应如下所示,使用
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);

绑定成功。设置PageName属性时,还会调用属性更改通知。但是在那之后它就不会被更新了。绑定成功了。设置PageName属性时,还会调用属性更改通知。但是在那之后它就不会被更新了。这也不起作用:frameBinding.Path=newpropertypath(“.”);frameBinding.Source=mainWindowViewModel.PageName;这也不起作用:frameBinding.Path=newpropertypath(“.”);frameBinding.Source=mainWindowViewModel.PageName;