0

特定の ICommand が実行された後、コントロールに視覚効果を加える必要があります。たとえば、カスタム コントロールは AAACommand および BBBCommand プロパティを公開します。

<myControl AAACommand={Binding ACommand}
           BBBCommand={Binding BCommand} />

ここで、ACommand と BCommand は ViewModel のコマンドです。AAACommand がいつ実行されたかを知る方法は? サブスクライブする ICommand の Executed イベントはありません。

編集: AAACommand は、ユーザー コントロールで次のように定義されています。

public static readonly DependencyProperty AAACommandProperty =
        DependencyProperty.Register("AddCommand", typeof(RelayCommand), typeof(MyCustomControl), null);

public static readonly DependencyProperty AAACommandParameterProperty =
        DependencyProperty.Register("AAACommandParameter", typeof(object), typeof(MyCustomControl), null);

public RelayCommand AAACommand
{
    get { return (RelayCommand)GetValue(AAACommandProperty); }
    set { SetValue(AAACommandProperty, value); }
}

public object AAACommandParameter
{
    get { return (object)GetValue(AAACommandParameterProperty); }
    set { SetValue(AAACommandParameterProperty, value); }
}

したがって、ViewModel で ACommand を呼び出すことに問題はありません。これは問題なく動作します。問題は、AAACommand が ACommand を実行するタイミングをユーザー コントロールがどのように認識し、その UI で何かを実行できるかということです。

4

1 に答える 1

0

ビュー モデルから返された応答と実行されたコマンドに基づいてユーザー コントロールを更新しますか? あるユーザーコントロールから別のユーザーコントロールに文字列値として渡したいという本質的に似た質問をしました。INotifyProperty Changed イベントを使用してこれを実現しました。ここで元の質問と解決策を読むことができます

コメントへの更新:

あなたのコメントに基づいて、2 つのうちの 1 つが起こる可能性があるようです。VM が応答する必要がない場合は、ビュー内の要素によって更新がトリガーされる可能性があります。これは、Binding ElementNameProperty を使用して行うことができます。これにより、本質的に、別の要素のアクションに基づいてプロパティをトリガー/変更できます。(1 つのフィールドにテキストを入力すると、別のコントロールに値が表示されます) msdn の説明と例を次に示します。

戻り値 (つまり、成功または失敗) に基づいて呼び出す必要がある場合、ViewModel には、UI 内の要素のプロパティに双方向でバインドされたプロパティ (bool など) が必要です。バインディングを処理するために ( IValueConverterを継承する) コンバーターを作成する必要がある場合がありますが、 INotifyProp Change を使用して、コントロール間またはコントロール内のバインドされた要素間の更新をマーシャリングします。

簡単な例を次に示します。

xaml 内に、セカンダリ ユーザー コントロール内の別のボタンがクリックされるまで UI 内に表示したくないユーザー コントロールを追加しました。これを処理するために、Visibility プロパティにバインドを設定します

<ctrl:LandingPage x:Name="ucLandingPage"
                                  Grid.Row="1" 
                                  DataContext="{Binding}"  
                                  Visibility="{Binding LandingPageVisibility, Mode=OneWay, Converter={StaticResource LandingPageVisibilityConverter}}"/>

ビューモデル内には、次のプロパティとコードがありました

// Default ctor
        public SearchViewModel()  
        {
            //Show that the Landing Page control is being displayed
            SearchVisibility = Visibility.Collapsed;
}

VMのプロパティ(私はInotifyがベースオブジェクトに含まれているSimpleMVVMフレームワークを使用しているので、私のnotify propイベントはあなたのものとは少し違うように見えるかもしれません)

 private Visibility _SearchVisibility;
            public Visibility SearchVisibility
            {
                get { return _SearchVisibility; }
                set
                {
                    _SearchVisibility = value;
                    NotifyPropertyChanged(m => m.SearchVisibility);
                }
            }

次に、このプロパティを更新した VM 内のメソッド

 public void GetSearchResult()
        {
            currentPage = 1;
            //Set the visibility of the search control in the center of the page
            SearchVisibility = Visibility.Visible;
            this.SearchHistory = this._DataModel.AddSearchHistoryItem(this.SearchTerm);
        }

最後に、戻り値を要素の正しいプロパティ値に変換するコンバーター クラス

  public class SearchVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null & System.Convert.ToString(value) == "Visible")
            {
                return Visibility.Visible;
            }
            else
            {
                return Visibility.Collapsed;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
于 2012-05-09T12:07:12.770 に答える