wpf 绑定到 ViewModel.SubClass.Property(子属性)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11619612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:41:17  来源:igfitidea点击:

Binding to ViewModel.SubClass.Property (sub-property)

wpfmvvmbinding

提问by Sonic Soul

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(); }}
}

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

如何在我的视图中绑定到 CurrentRecord 的 Notes 属性?我试过这个:

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

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

然而,这不起作用。我还尝试设置周围 StackPanel 的 DataContext:

<StackPanel DataContext="{Binding CurrentRecord}">

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

之后,我在我的 TextBox {Binding Notes} 和 {Binding Path=Notes} 中尝试过,但这些似乎都不起作用。

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

也许上面真的应该工作,而我在其他地方搞砸了?

Update

更新

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>();

Also i am seeing a binding error: 'Notes' property not found on 'object' ''MainViewModel'

我还看到一个绑定错误:在“对象”“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:

为了验证我有正确的 ViewModel 绑定,我只是直接在 viewmodel 上添加了这个属性:

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

and corresponding textblock in the view:

以及视图中相应的文本块:

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

this works as expected.

这按预期工作。

Great Success

巨大的成功

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 ..

感谢 Ryan,我能够找到问题所在。它不在属性本身中,而是在设置 CurrentRecord 的方式中。在我的 setter 中,我调用了 INotifyPropertyChange 处理程序,但其中包含该属性的旧名称。所以视图没有收到 CurrentRecord 通知,所以我猜 Notes 通知是不够的..

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

总之,这个符号是正确的:{Binding Path=CurrentRecord.Notes}

采纳答案by cguedel

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

以上应该有效,{Binding Path=CurrentRecord.Notes} 是正确的。您能检查一下您的数据上下文是否设置为您的视图模型吗?

Also check if your viewmodel implements INotifyPropertyChanged.

还要检查您的视图模型是否实现了 INotifyPropertyChanged。

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.

编辑:我刚刚创建了一个示例项目来重新创建它。无需实现 INotifyPropertyChanged,它仅在数据上下文设置为 VM 时起作用。

回答by Thelonias

Make sure that your CurrentRecord property is 1) being set and 2) notifying the UI layer that a change has occurred.

确保您的 CurrentRecord 属性 1) 正在设置和 2) 通知 UI 层发生了更改。