0

いくつかのボタンを含むがあり、UserControlボタンごとにCommand DependencyProperty定義されています。UserControl XAMLは次のとおりです(簡潔にするために一部の要素は削除されています)。

<UserControl>
   <Grid>
      <Button Command="{Binding CloseCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" ToolTip="Delete">
      <Button Command="{Binding NavigateCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" ToolTip="Launch">
   </Grid>
</UserControl>

背後にあるUserControlコードのDepencyPropertiesは次のとおりです。

public static readonly DependencyProperty CloseCommandProperty = DependencyProperty.Register("CloseCommand", typeof (ICommand), typeof (Tile));

public ICommand CloseCommand
{
    get { return (ICommand)GetValue(CloseCommandProperty); }
    set { SetValue(CloseCommandProperty, value); }
}

public static readonly DependencyProperty NavigateCommandProperty = DependencyProperty.Register("NavigateCommand", typeof (ICommand), typeof (Tile));

public ICommand NavigateCommand
{
    get { return (ICommand) GetValue(NavigateCommandProperty); }
    set { SetValue(NavigateCommandProperty, value); }
}

私はMVVMLightを使用してRelayCommandsおり、このいくつかのインスタンスを含むビューのViewModelを介してこれらのコマンドにバインドできますUserControlCommandParametersUserControlのコマンドごとに個別に公開できるようにしたいと思います。これは可能ですか?

将来的には、全体(基本的にはグリッド内に直接含まれるグリッド)で他のイベント(クリック、ドラッグ、データドロップ)のコマンドを公開したいと思いUserControlます。ここでも、それぞれが独自のを公開しCommandParametersます。

Commandマルチプルとペアリングについての情報があまりないCommandParameterので、間違ったアプローチを取っているのではないかと思います。CustomControlより適切でしょうか?または、これは?を使用して最もよく解決されDataTemplateますか?コマンドとしてグリッド上でクリックアンドドラッグなどのイベントを公開することも、あまりカバーされていないようです-私のニーズに合うより良いコンテナコントロールはありますか?

4

1 に答える 1

1

RelayCommandあなたはこのように受け入れるために作成することができますCommandParameter-

closeCommand = new RelayCommand<bool>((flag) => Close(flag));

private void Test(bool flag)
{
    if(flag)
       CloseWindow();
}

つまり、一般的なRelayCommand<T>実装を使用します。

Executeメソッドは、Action(または、コマンドにパラメーターがある場合はAction、この場合はRelayCommandクラスを使用する必要があります)の形式でRelayCommandのコンストラクターに渡されます。

Silverlight3およびWPFでのRelayCommandsの使用

参照。:-MVVMLightV4用の新しいRelayCommandスニペットを提案する

CommandParameter次に、のプロパティを使用してパラメータを渡しますButton

<Button Command="{Binding CloseCommand }" 
        CommandParameter="{Binding SomeCommandParameter}" ... />

または

<Button Command="{Binding CloseCommand }" 
        CommandParameter="True" ... />
于 2012-06-26T11:30:11.730 に答える