MVVM + UserControl +依赖属性 [英] MVVM + UserControl + Dependency Property

查看:208
本文介绍了MVVM + UserControl +依赖属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这与以下问题有关: WPF从单个视图模型打印多个页面

Alright, this is somewhat related to this question: WPF Printing multiple pages from a single View Model

我试图遵循那里给出的建议,但现在我陷入了困境。

I tried to follow the advice given there but now I am stuck.

我的应用程序使用MainView.xaml和适当的MainViewViewModel.cs,我在后台使用MVVM Light。

My application uses a MainView.xaml and the appropriate MainViewViewModel.cs, I am using MVVM Light in the background.

现在-根据帖子-看来我必须执行以下操作:

Now - according to the post - it seems I have to do the following:


  • 创建用户控件

  • 公开一些属性通过用户控件

  • 确保视图模型显示了这些属性

这个想法很明确但是我在尝试互相通知时陷入困境。

The idea is clear but I am stuck when trying to notify each other.

我的用户控件(UcTest.xaml)公开了一个依赖项属性:

My user control (UcTest.xaml) exposes a Dependency Property:

public string SpecialText
{
    get { return (string)GetValue(SpecialTextProperty); }
    set
    {
        SetValue(SpecialTextProperty, value);

    }
}

// Using a DependencyProperty as the backing store for SpecialText.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty SpecialTextProperty =
    DependencyProperty.Register("SpecialText", typeof(string), typeof(UcTest), new PropertyMetadata(new PropertyChangedCallback(SpecialTextChangedPropertyCallback)));

private static void SpecialTextChangedPropertyCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    // Do something
    Debug.WriteLine("Ffgdgf");
}

好,所以我现在有一个具有某些依赖项属性的用户控件。但是,这些属性与我的ViewModel属性是完全分开的(应该显示的属性)。

Alright, so I do now have a user control which has some dependency properties. Yet, these properties are completely separated from my ViewModel properties (those are the ones which shall be displayed).

所以基本上我有两种可能性:

So basically I have two possibilities:


  • 我现在如何告诉UserControl的ViewModel一些属性已更改?

  • 是否有可能忘记

其他信息#1:
我已经上传了一个(简单的)示例我在这里尝试做的事情:示例项目。我想从MainViewViewModel中更改UserControl1中标签的值(通过ViewControl中UserControl1的绑定属性)。

Additional info #1: I have uploaded a (simple) example of what I am trying to do here: Example Project. I would like to change the value of the label in UserControl1 (via the binding property in the ViewModel for UserControl1) from my MainViewViewModel.

推荐答案

好吧,经过数小时的搜寻之后,看来正确的方法根本没有做到这一点。通用方法是将数据保留在MainViewModel中,而不对UserControl使用其他ViewModel(我发现有点...嗯..不太好)。主要问题是,没有简单的机制可以将数据从Dependency属性获取到ViewModel。

Alright, after hours of googling it appears that the "correct" approach to this is not to do it at all. The general approach is to keep the data in your MainViewModel and not use an additional ViewModel for the UserControl (which I find a little ... well .. not so good). The main problem is that there is no easy mechanism to get the Data from the Dependency Property to the ViewModel.

对于打印,我现在已经回到纯粹在代码。

For printing, I have now gone back to doing it purely in code.

这篇关于MVVM + UserControl +依赖属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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