WPF ListBoxItem をダブルクリックしますか? 質問する

WPF ListBoxItem をダブルクリックしますか? 質問する

WPF ListBox には、少なくとも私が知る限り DoubleClick イベントはありません。この問題を回避するには、項目をダブルクリックしてイベント ハンドラーで何かを実行できるようにする方法がありますか? ご協力ありがとうございます。

ベストアンサー1

ListBoxItemパラメータ付きのコマンドをsにバインドすることは可能です。コードビハインドまたは付随行動InputBindings、単にMouseBinding前述のようにこの答え

forListBoxの例:MouseBindingLeftDoubleClick

<ListBox ItemsSource="{Binding MyDataSource}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding MySourceItemName}">
                <TextBlock.InputBindings>
                    <MouseBinding MouseAction="LeftDoubleClick"
                                  Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"
                                  CommandParameter="{Binding MySourceItemId}" />
                </TextBlock.InputBindings>
            </TextBlock>
        </DataTemplate> 
    </ListBox.ItemTemplate>
</ListBox>

ItemsSourceコマンドがのと同じ DataContext で定義されている場合は、例に含まれているバインディングをListBox使用してバインドできます。RelativeSource

おすすめ記事