0

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 Answers1

0

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.

Geert van Horrik
  • 5,689
  • 1
  • 18
  • 32
  • I can't get it to handle change notifications automatically. Is there anything that needs to be added in the app.xaml.cs? Or anything else you can think of that needs to be added? Ideas? – Johnny Beatniks Jul 06 '13 at 17:37
  • You are right. I tested it and it seems there is a bug in the WPF version with the ViewToViewModel mappings. We will look into this. I created an issue, see https://catelproject.atlassian.net/browse/CTL-112 – Geert van Horrik Jul 07 '13 at 19:22
  • Thank you. Starting to get the hang of this! – Johnny Beatniks Jul 08 '13 at 00:18
  • The bug is fixed and a new nightly release has been published via NuGet. See https://dl.dropboxusercontent.com/u/8455721/StackOverflow/17499366_ViewToViewModelExample.zip for a working example. – Geert van Horrik Jul 08 '13 at 17:36