0

手伝ってくれてどうもありがとう。

小さな例を動作させることで、ViewToViewModel 属性を理解しようとしています。私はいくつかの質問に行きました。私のコードは以下です。

  1. [ViewToViewModel] 属性は、View、ViewModel、またはその両方に配置する必要がありますか?

  2. 次のような MappingType 属性を使用しようとすると、[ViewToViewModel, MappingType = ...] MappingType でエラーが発生します。「使用」ステートメント/アセンブリ参照がありませんか? 構文の例はありますか?

  3. 必要な方法で動作させることはできますが、「ViewToViewModel」部分が正しく動作しているとは思いません。ユーザー コントロールのコード ビハインドでは、プロパティの変更は HandleMyName(object e) で処理されます。ViewToViewModel はこれを行うべきですか?

ビュー:

  • メインウィンドウ
  • ユーザー コントロール ビュー

ビューモデル:

  • メインウィンドウViewModel
  • UserControlViewViewmodel

メインウィンドウ

<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()
    {

    }
}

}

4

1 に答える 1

0

1) ViewToViewModel はビューに配置する必要があります (VM を汚染したくありません)。

2) 属性は [ViewToViewModel(MappingType = ...)] として使用する必要があります。

3) ViewToViewModel は、ビューのプロパティ x からビュー モデルのプロパティ x への自動マッピングを処理する必要があります。すべての変更通知を自動的に処理します。

于 2013-07-06T12:37:29.097 に答える