带有单选按钮的ListView仅在右键单击时触发/引发SelectionChanged(需要左键单击)
本文关键字:单击 SelectionChanged 引发 单选按钮 ListView 右键 | 更新日期: 2023-09-27 18:11:09
我有一个ListViewItem的XAML/Control模板:
<Window.Resources>
<ControlTemplate x:Key="ItemTemplate" TargetType="ListViewItem">
<Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="0,5,0,5" BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Panel.Background}" SnapsToDevicePixels="True">
<ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</Border>
</ControlTemplate>
<Style TargetType="ListViewItem">
<Setter Property="Template" Value="{StaticResource ItemTemplate}" />
</Style>
<DataTemplate x:Key="ItemDataTemplate">
<RadioButton
x:Name="radiobutton" GroupName="LeGroup"
Content="{Binding Path=Name}" Checked="radiobutton_Checked" />
</DataTemplate>
</Window.Resources>
并且当我尝试将SelectionChanged设置为fire时,它仅在用鼠标右键单击选中单选按钮时才会触发。当它是一个正常的左键点击时,我需要它触发。
<ListView x:Name="radioListView" SelectionMode="Single" ItemsSource="{Binding}" ItemTemplate="{StaticResource ItemDataTemplate}" SelectionChanged="radioListView_SelectionChanged" Loaded="radioListView_Loaded"></ListView>
似乎找不到任何关于在右键单击时强制selectionchanged事件的内容。标准是左键点击。
提前谢谢你:)
编辑:所以我注意到,SelectionChanged事件触发当我点击左键的单选按钮(及其相关文本)的OUTSIDE。就好像单选按钮独立于列表视图工作一样。SelectionChanged事件在单选按钮元素内的右键单击时触发。真奇怪,我还在想。
我不知道为什么鼠标左键不起作用,但这是我用于显示带有RadioButtons的ListBox的样式。鼠标右键和鼠标左键都可以正常工作
<Style x:Key="ListBoxAsRadioButtonsStyle" TargetType="{x:Type ListBox}">
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2, 2, 2, 0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Background="Transparent">
<RadioButton IsHitTestVisible="False" Focusable="false"
Content="{TemplateBinding ContentPresenter.Content}"
IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
像
一样使用<ListBox ItemsSource="{Binding MyItems}"
Style="{StaticResource ListBoxAsRadioButtonsStyle}" />
如果我不得不猜测你的问题,我猜常规的ListView选择行为是标记LeftMouseClick事件作为处理当你点击一个项目
好奇怪!选择发生在右键点击??
我所能建议你的是采取一种无事件的方法。你可以通过绑定SelectedItem/SelectedValue等属性来使用MVVM模式。
你也可以利用ListView的单一选择模式来自动"分组"单选按钮,以便每次只选择一个
。单选按钮的IsChecked
绑定到祖先ListViewItem的IsSelected
属性,两种方式。这样,当选中单选按钮时,它将自动选择列表视图行,SelectedItem'Value绑定将自动触发。在ListView的单选择模式下,点击下一个单选按钮将自动取消前一行的选择。