2016-08-21 2 views
0

テンプレート10とMVVMを使用してUWPアプリケーションを作成しています。私の要件は、独自のViewModelを持つだけでなく、独自の依存プロパティを持つUserControlを作成することです。独自のViewModelを持つUWP UserControlとテンプレート10を使用する依存関係プロパティ

私の要件:ボタンのクリックで

  1. コール親ViewModelにコマンド。
  2. バインドTexBlockテキストからユーザーコントロールのViewModel

私のユーザーコントロールは、次のようになります。

<vm:MyUserControl1 AddItem="{Binding MyCommand}" Component="{Binding}" RelativePanel.Below="abc" /> 

ユーザーコントロールのXAML:

:これは、コードの後ろにユーザーコントロールのコードが

<StackPanel> 
      <TextBlock Text="{x:Bind Component.Text, Mode=OneWay}"/> 
      <Button x:Name="Button" Content="Click Me" Command="{x:Bind AddItem}"> 
      </Button> 
</StackPanel> 

です

public sealed partial class MyUserControl1 : UserControl 
    { 
     public MyUserControl1() 
     { 
      this.InitializeComponent(); 
      // mygrid.DataContext = this; 
      (this.Content as FrameworkElement).DataContext = this; 
     } 

     public static readonly DependencyProperty AddItemProperty = 
      DependencyProperty.Register(
       "AddItem", 
       typeof(ICommand), 
       typeof(MyUserControl1), new PropertyMetadata(null)); 

     public ICommand AddItem 
     { 
      get { return (ICommand)GetValue(AddItemProperty); } 
      set { SetValue(AddItemProperty, value); } 
     } 

     public static readonly DependencyProperty ComponentProperty = DependencyProperty.Register("Component",typeof(MyViewModel),typeof(MyUserControl1),new PropertyMetadata(null)); 

     public MyViewModel Component 
     { 
      get { return (MyViewModel)GetValue(ComponentProperty); } 
      set { SetValue(ComponentProperty, value); } 
     } 

    } 

ユーザーコントロールビューモデル:

public class MyViewModel:ViewModelBase 
    { 
     public MyViewModel() 
     { 

     } 
     public string Text => "ABC"; 
    } 

親ビューモデル:ここで間違って行くいただきまし

Unable to cast object of type 'WindowsApp2.ViewModels.SettingsPartViewModel' to type 'WindowsApp2.ViewModels.MyViewModel'. 

public class SettingsPartViewModel : ViewModelBase 
    { 
     DelegateCommand _MyCommand; 

     public DelegateCommand MyCommand 
      => _MyCommand ?? (_MyCommand = new DelegateCommand(async() => 
      { 
       await Task.Run(() => { 
        ///Some Code 
       }); 
      })); 
    } 

私は、コードを実行するたびに、私はエラーを次のですか?

答えて

0

ここで間違っていると思います。Component="{Binding}"親ビューのDataContextに直接コンポーネントを設定しています。つまり、WindowsApp2.ViewModels.MyViewModelではなくWindowsApp2.ViewModels.SettingsPartViewModelです。

正しく動作させるには、SettingsPartViewModelMyViewModelのインスタンスを作成してComponentにバインドする必要があります。サンプル以下

試してみてください。SettingsPartViewModelで

:SettingsPart.xamlで

private MyViewModel myViewModelInstance = new MyViewModel(); 

public MyViewModel MyViewModelInstance 
{ 
    get { return myViewModelInstance; } 
    set { myViewModelInstance = value; //Raise NotifyPropertyChanged } 
} 

<vm:MyUserControl1 AddItem="{Binding MyCommand}" Component="{Binding MyViewModelInstance}" RelativePanel.Below="abc" /> 
+0

はあなたのソリューションをありがとうございました。その期待通りに働いています。しかし、今私が直面している問題はUIに反映されないMyViewModelプロパティの値を変更する場合です。私はComponent = "{MyViewModelInstance、Mode = OneWayをバインドする"}をやってみましたが、まだ運がありません。助けてもらえますか –

+0

あなたのコードを見ることなく、私はあなたを助けることができません。 'Mode = TwoWay'を試して、INotifyPropertyChangedインターフェースが正しく実装されているかどうかを確認してください。 – ViVi

+1

完了。 ComponentプロパティモードをOneWayに設定すると、Text = "{x:Bind Component.Text、Mode = OneWay}"という問題が解決されます。すべてが魅力のように働いています。ありがとう! –

関連する問題