将对象值从MainViewModel更改为用户控件WPF

时间:2015-04-20 11:41:56

标签: c# wpf xaml mvvm

我有一个usercontrol,它绘制一个选择矩形,它具有一个包含选择矩形值的Selection属性。选择在我的主窗口中以两种方式绑定,因此ViewModel可以访问selectionRectangle值。现在我想通过更改ViewModel中的Selection值来更改selectionRectangle的大小。我正在尝试实现一个PropertyChangedCallback来执行此操作,但我不够了解这一点。我目前拥有的是一个OnSelectionChanged方法,如果我的PropertyChangedCallback被触发,它将为我的selectionRectangle设置新值。以下是我的代码片段

在UserControl依赖属性和OnSelectionChanged方法中:

      public static readonly DependencyProperty SelectionProperty =
        DependencyProperty.Register("Selection", typeof(Rect), typeof(CropControl), new PropertyMetadata(default(Rect))); 

    public Rect Selection
    {
        get 
        { 
            return (Rect)GetValue(SelectionProperty); 
        }
        set 
        {
            //if(this.Selection!=value)
                SetValue(SelectionProperty, value);
        }
    }

    // this is used, to react on changes from ViewModel. If you assign a  
    // new Rect in your ViewModel you will have to redraw your Rect here
    private static void OnSelectionChanged(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e)
    {
        Rect newRect = (Rect)e.NewValue;
        Rectangle selectionRectangle = d as Rectangle;

        if (selectionRectangle != null)
            return;

        selectionRectangle.SetValue(Canvas.LeftProperty, newRect.X);
        selectionRectangle.SetValue(Canvas.TopProperty, newRect.Y);
        selectionRectangle.Width = newRect.Width;
        selectionRectangle.Height = newRect.Height;
    }

在我的mainviewmodel中,没有什么特别的Selection属性可以获取并设置矩形的值。

MainViewModel Selection属性:

     private Rect selection;     //defines a Rect variable

    //getset accessor which is bound to views UserControl that gets the dimension of the rectangle from CropControl
    //and sets this dimension to private variable selection. The dimension will then be used to display the area 
    //cropped by the user.
    public Rect Selection
    {
        get
        {
            return selection;
        }
        set
        {
            selection = value;

            //// Or whatever the name of your framework/implementation the method is called
           SetPropertyChanged("Selection");

            //// Cause ICommands to reevaluate their CanExecute methods
            CommandManager.InvalidateRequerySuggested();
        }
    }

现在在DependencyProperty.Register中,我尝试重写它并添加FrameworkPropertyMetadata,因为更改发生在另一个类中,但我不确定要使用哪个对象默认值,或者我是否正确这样做。

    public static readonly DependencyProperty SelectionProperty =
        DependencyProperty.Register("Selection", typeof(Rect), typeof(CropControl), new PropertyMetadata(default(Rect)), new FrameworkPropertyMetadata(null,new PropertyChangedCallback(OnSelectionChanged));

这会给出重载方法匹配错误。

1 个答案:

答案 0 :(得分:0)

您无需创建或注册依赖项属性即可完成您要执行的操作。您所要做的就是从主视图模型中提升INotifyPropertyChanged接口的PropertyChanged事件。以下是我通常如何处理此问题的示例代码。

public class MainViewModel : INotifyPropertyChanged
{
  // This event is for the INotifyPropertyChanged interace
  public event PropertyChangedEventHandler PropertyChanged;

  // This is a helper method that makes it straightforward to raise the PropertyChanged event
  public void NotifyPropertyChanged(string propertyName)
  {
    PropertyChangedEventHandler handler = this.PropertyChanged;

    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
  } 

  private Rect selection;
  public Rect Selection
  {
    get
    {
      return this.selection;
    }
    set
    {
      this.selection = value;

      // Alert the View that the property has been changed
      this.NotifyPropertyChanged("Selection");
    }
  }
}

只要您在视图模型中设置Selection的值,任何绑定到该属性的UI元素都将自动更新。