绑定到ViewModel.SubClass.Property(子属性) [英] Binding to ViewModel.SubClass.Property (sub-property)

查看:187
本文介绍了绑定到ViewModel.SubClass.Property(子属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有我的屏幕,在当前记录编辑上一节..所以我的视图模型与所有当前编辑的特性,如类:

let's say i have a section on my screen where "current record" is edited.. so my view model has a class with all currently edited properties such as:

class Record { 
    public string Notes { get { return "Foo"; } set { _notes = value; Notify("Notes"); }
}

和我们这个类添加到视图模型:

and we add this class to the view model:

class AdjustsmentViewModel {
    public Record CurrentRecord { get { return new Record(); }}
}

我如何可以绑定到CurrentRecord的Notes属性在我的看法?我想这样的:

How can i bind to Notes property of CurrentRecord in my view? I tried this:

<TextBox Text="{Binding CurrentRecord.Notes, Mode=TwoWay}" VerticalScrollBarVisibility="Auto" TextWrapping="WrapWithOverflow" AcceptsReturn="True"  />

这是不但是工作。我也试着设置周围的StackPanel的DataContext的:

This is not working however. I also tried setting DataContext of surrounding StackPanel:

<StackPanel DataContext="{Binding CurrentRecord}">

在那之后,我想在我的文本框{结合注}和{绑定路径=注},但这些都不似乎工作。

After that, i tried in my TextBox {Binding Notes} and {Binding Path=Notes}, but none of these seem to work.

以上或许真应的工作,我在其他地方搞乱的东西吗?

Perhaps above really should work and i am messing something elsewhere?

更新

这是发生在用户控件。此用户控件有一个单独的视图模式,从它的父窗口。

This is happening in a user control. This user control has a separate view model, from it's parent window.

this.DataContext = UnityUtil.Resolve<AdjustmentsViewModel>();

另外,我看到绑定错误:'注意'属性的'对象'''MainViewModel

这视图模型设置在主窗口中。

that view model is set on the main window.

要验证我有权利约束视图模型,我只是直接对视图模型添加此属性:

to verify that i have the right ViewModel bound, i just added this property directly on the viewmodel:

public string Notes2 { get { return "Bar"; } } 

和视图中的相应的文本块:

and corresponding textblock in the view:

<TextBlock Text="{Binding Path=Notes2}" />

这正常工作。

大成功

感谢瑞安,我能发现问题。这不是在房地产本身,而是一路CurrentRecord已被设置。在我的二传手,我做出INotifyPropertyChange处理程序的调用,但是有它的属性的旧名。因此,有观点没有得到CurrentRecord通知,所以我想说明的通知是不够的。

Thanks to Ryan, i was able to find the problem. It was not in the property itself, but the way CurrentRecord was being set. In my setter, i make a call to INotifyPropertyChange handler, but that had the old name of the property in it. So the view was not getting a CurrentRecord notification, so i guess Notes notification was not enough ..

在结论中,这个符号是正确的: {结合路径= CurrentRecord.Notes}

in conclusion, this notation is correct: {Binding Path=CurrentRecord.Notes}

推荐答案

以上应该工作,{绑定路径= CurrentRecord.Notes}是正确的。你可以检查你的datacontext设置为您的视图模型?

The above should work, {Binding Path=CurrentRecord.Notes} is right. Can you check that your datacontext is set to your viewmodel?

此外检查您的视图模型实现INotifyPropertyChanged。

Also check if your viewmodel implements INotifyPropertyChanged.

编辑:
我刚刚创建了一个示例项目重新创建此。无需实现INotifyPropertyChanged,当DataContext设置到VM它只是工作。

edit: I just created a sample project to recreate this. No need to implement INotifyPropertyChanged, it just works when the datacontext is set to the VM.

这篇关于绑定到ViewModel.SubClass.Property(子属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆