【问题标题】:Silverlight UI element's 1-time binding to nested non-dependency property failsSilverlight UI 元素的 1 次绑定到嵌套的非依赖属性失败
【发布时间】:2025-12-06 09:15:01
【问题描述】:

我想在 childWindow 中的复选框(IsChecked 属性)和它的 DataContext 对象中的嵌套成员变量之间进行 1 次数据绑定,这样只有 initial IsChecked 属性由成员变量设置.成员变量(绑定源)不是 INotifyPropertyChanged 或标记为 DependencyProperty- 但我认为没关系 b/c 我只希望它被评估一次 - 当它获得初始值时。

绑定(在 testChildWindow.xaml 中):

<CheckBox Content="Show Username?" Name="cbShowUser" IsChecked="{Binding Path=User.showUser}"/>

设置 DataContext(在父窗口代码隐藏中):

  testChildWindow dlgBox = new testChildWindow();
  dlgBox.DataContext = (this.DataContext as IAssignDlgViewModel).AssignVM("defaultChildWindow");
  dlgBox.Show();

数据上下文/成员变量:

public class testChildWindowViewModel : IDlgViewUpdate 
{
  public User
  ...
}

public class User 
{
  public bool showUser;
  public User()
  {
    showUser = true;
  }
  ...
}

如果我将 Vm 的绑定源属性 (showUser) 设为(非嵌套)testChildWindowViewModel 的依赖属性,则绑定有效。但所有其他组合似乎都失败了。

为什么它必须是 1 次绑定的依赖项(或 INotifyPropertyChanged?)属性?

为什么我不能让它在嵌套级别工作?

谢谢!!!

【问题讨论】:

    标签: wpf silverlight data-binding binding dependency-properties


    【解决方案1】:

    啊,在绑定过程中查看输出窗口为我回答了这个问题。问题是 User 不是属性。将其更改为自动属性,并且绑定现在可以正常工作。

    【讨论】:

      最近更新 更多