Catel ViewToViewModel attribute

走远了吗. 提交于 2019-12-12 02:24:37

问题


Thank you so much for your help.

I'm trying to understand the ViewToViewModel attribute by getting a small example to work. I've go a couple of questions. My code is below.

  1. Is the [ViewToViewModel] attribute supposed to be placed to be placed in the View, ViewModel or both?

  2. If I try to use an attribute, MappingType, such as: [ViewToViewModel, MappingType = ...] MappingType gives me an error. Am I missing a "using" statement/Assembly Reference? Is there an example of syntax?

  3. I'm able to get things to work the way I need, but I don't think that I'm getting the "ViewToViewModel" part to work properly. In the codebehind of the usercontrol, property changes are handled in HandleMyName(object e). Is ViewToViewModel supposed to do this?

Views:

  • MainWindow
  • UserControlView

ViewModels:

  • MainwindowViewModel
  • UserControlViewViewmodel

MainWindow

<catel:DataWindow x:Class="ViewToViewModelStudy.Views.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:catel="http://catel.codeplex.com"
              xmlns:uc="clr-namespace:ViewToViewModelStudy.Views" >
  <StackPanel x:Name="LayoutRoot">
    <Label Content="{Binding Title}" />
    <uc:UserControlView MyName="{Binding Title}"  />
  </StackPanel>
</catel:DataWindow>

.

UserControlView.xaml

<catel:UserControl x:Class="ViewToViewModelStudy.Views.UserControlView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:catel="http://catel.codeplex.com">
  <StackPanel>
    <TextBlock>Innerview Model</TextBlock>
    <TextBlock Text="{Binding MyName}"></TextBlock>
    <TextBlock>Innerview Model</TextBlock>
  </StackPanel>
</catel:UserControl>

UserControlView.xaml.cs

namespace ViewToViewModelStudy.Views
{
   using Catel.Windows.Controls;
   using Catel.MVVM.Views;
   using System.Windows;
   using System.Data;

public partial class UserControlView : UserControl
{
    [ViewToViewModel]
    public string MyName
    {
        get { return (string)GetValue(MyNameProperty); }
        set { SetValue(MyNameProperty, value); }
    }

    public static readonly DependencyProperty MyNameProperty =
       DependencyProperty.Register(
       "MyName",
       typeof(string),
       typeof(UserControlView),
     new FrameworkPropertyMetadata(null,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnMyName)));

    static void OnMyName(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        UserControlView ic = (UserControlView)obj;
        ic.HandleMyName(e.NewValue);
    }

    private void HandleMyName(object e)
    {
           ViewModels.UserControlViewModel vm = (ViewModels.UserControlViewModel)this.ViewModel;

           if (vm != null)
           {
               vm.MyName = e.ToString();  // << Shouldn't this happen automagically?
           }
    }

    public UserControlView()
    {
        InitializeComponent();
    }

  }
}

UserControlViewModel.cs

namespace ViewToViewModelStudy.ViewModels
{

    using Catel.MVVM;
    using Catel.Data;
    using Catel.MVVM.Views;
    using Catel.Windows.Controls;

public class UserControlViewModel : ViewModelBase
{
    public UserControlViewModel()
    { }

    public string MyName
    {
        get { return GetValue<string>(MyNameProperty); }
        set { SetValue(MyNameProperty, value); }
    }

    public static readonly PropertyData MyNameProperty = RegisterProperty("MyName", typeof(string), null, (sender, e) => ((UserControlViewModel)sender).OnMyPropertyChanged());

    private void OnMyPropertyChanged()
    {

    }
}

}


回答1:


1) A ViewToViewModel should be located in the view (you don't want to pollute your VM with it).

2) The attribute should be used as [ViewToViewModel(MappingType = ...)]

3) The ViewToViewModel should handle the automatic mapping of property x on the view to property x on the view model. It will handle all change notifications automatically.



来源:https://stackoverflow.com/questions/17499366/catel-viewtoviewmodel-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!