ItemsControl 中 CheckBox 的 MiddleClick 的 WPF 命令参数
WPF command parameter for MiddleClick on CheckBox in ItemsControl
我想在 ItemsControl
中的复选框上使用 MiddleClick 时触发命令。我需要 return 项目源作为命令参数。我在XAML.
中尝试了两种方法
方法一:
<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>
此方法return将 UserControl 添加到命令而不是项目源。
方法二:
<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>
此方法不会触发 SelectOnlyCommand。感谢您的帮助。
(已编辑)
如果你想传递单项,它是这样的:
<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>
你可以通过方法 2 实现,但问题是你无法绑定 Command。您必须通过 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>
我想在 ItemsControl
中的复选框上使用 MiddleClick 时触发命令。我需要 return 项目源作为命令参数。我在XAML.
方法一:
<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>
此方法return将 UserControl 添加到命令而不是项目源。
方法二:
<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>
此方法不会触发 SelectOnlyCommand。感谢您的帮助。
(已编辑) 如果你想传递单项,它是这样的:
<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>
你可以通过方法 2 实现,但问题是你无法绑定 Command。您必须通过 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>