问对WPF MVVM模式? [英] Question on MVVM pattern on WPF?

查看:117
本文介绍了问对WPF MVVM模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件让说的 UC1 。这个用户控件具有视图模型 UC1_vm。

I have a user control let say UC1 . This user control have viewmodel UC1_vm.

在用户控件UC1我在其中绘制曲线逻辑实现的画布。该图曲线逻​​辑基于在视图模型(UC1_vm)数据点属性。

In the usercontrol UC1 I have a canvas in which drawing curve logic is implemented. This drawing curve logic is based on the data points property in the view model ( UC1_vm).

数据点与不同条件下的视图模型的变化里面财产。数据点的生成被写入视图模型

The data points property inside the view model change with different condition. The generation of data points is written in the View Model.

我要绑定的视图模型到用户控件(视图)内画曲线逻辑数据点属性。我希望每当数据点属性在视图模型改变,画布调用draw曲线法。

I want to bind the data points property in the view model to the draw curve logic inside the User control (view). I want whenever the data point property is changed in the view model , the canvas calls the draw curve method.

我可以设置设置画布的任何有关财产的时候改变它调用油漆逻辑自动matically?

Can I set the set any property of canvas which when changed it calls the on paint logic auto matically?

请给我建议实施此方案!!的做法

Please suggest me the approach of implementing this scenario!!

推荐答案

这听起来像你有一个DependencyProperty的是点的集合在你的用户控件。当你注册,使用 FrameworkPropertyMetadata 元数据和元数据的构造函数指定 FrameworkPropertyMetadataOptions.AffectsRender 。请注意,如果整个集合被替换,这只会工作(如果你提出的PropertyChanged收集,但集合实例并没有改变,漆面仍然不会被调用)。

It sounds like you have a DependencyProperty that is the collection of points in your UserControl. When you register it, use the FrameworkPropertyMetadata metadata, and specify the FrameworkPropertyMetadataOptions.AffectsRender in the metadata constructor. Note that this will only work if the entire collection is replaced (if you raise PropertyChanged for the collection, but the collection instance hasn't changed, your paint still won't get called).

如果您的收藏农具 INotifyCollectionChanged ,那么你可以线了一个集合更改事件处理程序无效视觉:

If your collection implements INotifyCollectionChanged, then you can wire up a collection changed event handler that invalidates the visual:

public static DependencyProperty PointsProperty = DependencyProperty.Register(
    "Points",
    typeof(IEnumerable<Point>),
    typeof(UC1),
    new FrameworkPropertyMetadata(null, 
        FrameworkPropertyMetadataOptions.AffectsRender,
        OnPointsChanged));

public IEnumerable<Point> Points
{
    get { return (IEnumerable<Point>)GetValue(PointsProperty); }
    set { SetValue(PointsProperty, value); }
}

private static void OnPointsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    UC1 ctrl = d as UC1;
    if (e.NewValue != null && e.NewValue is INotifyCollectionChanged)
        ((INotifyCollectionChanged)e.NewValue).CollectionChanged += ctrl.PointsChanged;

    if (e.OldValue != null && e.OldValue is INotifyCollectionChanged)
        ((INotifyCollectionChanged)e.OldValue).CollectionChanged -= ctrl.PointsChanged;
}

private void PointsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    InvalidateVisual();
}

这篇关于问对WPF MVVM模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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