如何在 XAML 中访问 ViewModel 的 DependencyProperties?

发布于 2025-01-01 01:21:19 字数 760 浏览 1 评论 0原文

我目前正在使用 MVVM 模式编写一个用户控件,它具有一些属性,例如 Document。

ViewModel 中的 DependencyProperty

public static readonly DependencyProperty DocumentProperty = DependencyProperty.Register("Document", typeof(MyDocument), typeof(ResultControlViewModel), new PropertyMetadata(OnDocumentChanged));

        public MyDocument Document
        {
            get { return (MyDocument)GetValue(DocumentProperty); }
            set { SetValue(DocumentProperty, value); }
        }

使用用户控件的 MainView

<control:ResultControl x:Name="myControl" />

我如何使用 ViewModel 中的属性“Document”将它们在 XAML 中绑定到列表框中的选定项目例如主视图?

以编程方式。我可以在用户控件的代码隐藏中编写一个方法,但我认为这不是最好的方法。尤其是关于MVVM模式的使用。

I am currently writing a user control with the MVVM pattern which has some Properties, e.g. Document.

DependencyProperty in the ViewModel

public static readonly DependencyProperty DocumentProperty = DependencyProperty.Register("Document", typeof(MyDocument), typeof(ResultControlViewModel), new PropertyMetadata(OnDocumentChanged));

        public MyDocument Document
        {
            get { return (MyDocument)GetValue(DocumentProperty); }
            set { SetValue(DocumentProperty, value); }
        }

MainView which use the User Control

<control:ResultControl x:Name="myControl" />

How can I use my property "Document" from the ViewModel to bind them in XAML against the selected item of a ListBox in the MainView for example?

Programmaticlly. I can write a method in the code-behind of my user control, but this is I think not the beautiful way to do that. Especially with regard to the use of MVVM pattern.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的往事 2025-01-08 01:21:19

假设 MainViewModel 类具有 Documents 和 Document(即当前文档)属性,则 XAML 应如下所示:

<ListBox ItemsSource={Binding Path=Documents}, SelectedItem={Binding Path=Document}>
...
</ListBox>

<control:ResultControl DataContext={Binding Path=Document, Mode=OneWay} />

Assuming that MainViewModel class have Documents and Document (i.e. current document) properties, the XAML should look like:

<ListBox ItemsSource={Binding Path=Documents}, SelectedItem={Binding Path=Document}>
...
</ListBox>

<control:ResultControl DataContext={Binding Path=Document, Mode=OneWay} />
白龙吟 2025-01-08 01:21:19

我不太确定你在追求什么。您的意思是您的列表框是“文档”ViewModel 的集合吗?如果是这样,您可以将 UserControl 绑定到选定的“文档”:

<ListBox x:Name="MyListBox" ItemsSource="{Binding MyDocumentCollection}" />

<control:ResultControl x:Name="myControl" DataContext={Binding ElementName="MyListBox", Path="SelectedItem"}/>

编辑:Serge 的答案对于 MVVM 更好。将所选项目作为 ViewModel 的属性。

I'm not quite sure what you're after. Do you mean your ListBox is a collection of "Document" ViewModels? If so you can bind your UserControl to the selected "Document" with:

<ListBox x:Name="MyListBox" ItemsSource="{Binding MyDocumentCollection}" />

<control:ResultControl x:Name="myControl" DataContext={Binding ElementName="MyListBox", Path="SelectedItem"}/>

EDIT: Serge's answer is better with regards to MVVM. Having the selected item as a property on your ViewModel.

却一份温柔 2025-01-08 01:21:19

您需要将 Document 属性绑定到视图模型中的属性:

<control:ResultControl x:Name="myControl" Document="{Binding VmDocument}"/>

在您的 ViewModel 中:

public MyDocument VmDocument {get;set;}

当然,VmDocument 需要在其设置器上引发 PropertyChanged 事件。

You need to bind the Document property to a property in your viewmodel:

<control:ResultControl x:Name="myControl" Document="{Binding VmDocument}"/>

And in your ViewModel:

public MyDocument VmDocument {get;set;}

Of course, VmDocument needs to raise the PropertyChanged event on its setter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文