lundi 8 mars 2021

WPF command parameter for MiddleClick on CheckBox in ItemsControl

I want to trigger a command when I use a MiddleClick on a Checkbox in an ItemsControl. I need to return the item source as a command parameter. I have tried two methods in XAML.

Method 1:

            <ItemsControl x:Name="CheckBoxItems" ItemsSource="{Binding Curves}" Grid.Row="1">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel IsItemsHost="True"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}" Margin="0,0,5,0"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.InputBindings>
                    <MouseBinding Gesture="MiddleClick" Command="{Binding SelectOnlyCommand}"
                                  CommandParameter="{Binding }"/>
                </ItemsControl.InputBindings>
            </ItemsControl>

This method returns the UserControl to the command instead of the item source.

Method 2:

            <ItemsControl x:Name="CheckBoxItems" ItemsSource="{Binding Curves}" Grid.Row="1">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel IsItemsHost="True"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}" Margin="0,0,5,0">
                            <CheckBox.InputBindings>
                                <MouseBinding Gesture="MiddleClick" Command="{Binding Path=SelectOnlyCommand}"
                                              CommandParameter="{Binding }"/>
                            </CheckBox.InputBindings>
                        </CheckBox>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                
            </ItemsControl>

This method does not trigger the SelectOnlyCommand. Thanks for your help.




Aucun commentaire:

Enregistrer un commentaire