0

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.

1
  • So you just want to have the Curves passed as parameter? Because for that you can just simply write CommandParameter="{Binding Curves}" Commented Mar 8, 2021 at 23:52

2 Answers 2

1

(Edited) if you want to pass the single item, it works like this:

 <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 ElementName=CheckBoxItems, Path=DataContext.SelectOnlyCommand}"
                                          CommandParameter="{Binding }"/>
                        </CheckBox.InputBindings>
                    </CheckBox>
                </DataTemplate>
            </ItemsControl.ItemTemplate>                
        </ItemsControl>
Sign up to request clarification or add additional context in comments.

4 Comments

VS1 and vs2 return the full collection, instead of the single source.
Are you sure he wants the single item? I read it as if he wanted the whole collection aka "ItemsSource"
@spainchaud Sorry, you're the one that asked, lol.... See my new version and also the one of YoChauhan that use ElementName...to link to the "outer" DataContext
Work perfect now. Thanks. My users want a middleclick to turn off all checkboxes except the one clicked.
1

You can achieve that with way 2 , but problem is you not able to bind Command. You will have to bind it by ElementName.

  <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}" Margin="0,0,5,0">
                        <CheckBox.InputBindings>
                            <MouseBinding Gesture="MiddleClick" Command="{Binding ElementName=CheckBoxItems, Path = DataContext.SelectOnlyCommand}"
                                          CommandParameter="{Binding }"/>
                        </CheckBox.InputBindings>
                    </CheckBox>

1 Comment

He can also use his CheckBox "CheckBoxItems" as ElementName, its DataContext has that SelectOnlyCommand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.