67

テキストブロック(または場合によっては画像も-いずれにしても、そのユーザーコントロール)のダブルクリックイベントをViewModelのコマンドにバインドする必要があります。

TextBlock.InputBindingsが私のコマンドに正しくバインドされていないようです、何か助けはありますか?

4

4 に答える 4

283
<Button>
<Button.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="YourCommand" />
</Button.InputBindings>
</Button>

http://thejoyofcode.com/Invoking_a_Command_on_a_Double_Click_or_other_Mouse_Gesture.aspx

于 2011-07-30T10:42:27.553 に答える
8

Marlon Grech の付属のコマンド動作を試してください。

于 2009-08-18T12:39:34.957 に答える
7

簡単です。MVVM の方法を使用しましょう。ここでは、習得が容易で強力な MVVM Light を使用しています。

1. xmlns 宣言に次の行を追加します。

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"  
xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;
                                   assembly=GalaSoft.MvvmLight.Extras.WPF4"

2. テキストブロックを次のように定義します。

<textBlock text="Text with event">
   <i:Interaction.Triggers>
      <i:EventTrigger EventName="MouseDoubleClick">
         <GalaSoft_MvvmLight_Command:EventToCommand 
                             Command="{Binding Edit_Command}"/>
      </i:EventTrigger>
   </i:Interaction.Triggers>
</textBlock>

3.次に、ビューモデルにコマンドコードを記述します!!!

ViewModel1.cs

Public RelayCommand Edit_Command
{
   get;
   private set;
}

Public ViewModel1()
{
   Edit_Command=new RelayCommand(()=>execute_me());
}

public void execute_me()
{
   //write your code here
}

Real ERPアプリケーションで使用したので、うまくいくことを願っています

于 2011-11-30T11:37:39.150 に答える
2

リストビューの MouseDoubleClick イベントを ViewModel のコマンドにバインドする必要があるという同様の問題もありました。

私が思いついた最も簡単な解決策は、目的のコマンド バインドを持つダミー ボタンを配置し、MouseDoubleClick イベントのイベント ハンドラーでボタンのコマンドの Execute メソッドを呼び出すことです。

.xaml

 <Button Visibility="Collapsed" Name="doubleClickButton" Command="{Binding Path=CommandShowCompanyCards}"></Button>
                <ListView  MouseDoubleClick="ListView_MouseDoubleClick" SelectedItem="{Binding Path=SelectedCompany, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" Margin="0,10,0,0" ItemsSource="{Binding Path=CompanyList, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" HorizontalContentAlignment="Stretch" >

コードビハインド

     private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
            {
                doubleClickButton.Command.Execute(null);
            }

簡単ではありませんが、とてもシンプルで機能します。

于 2010-04-15T08:56:50.490 に答える