带有单选按钮的 ListView 仅在右键单击时触发/引发 SelectionChanged (需要左键单击)
我有以下用于 ListViewItem 的 XAML/控制模板:
<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 设置为触发时,它仅在通过鼠标右键单击选中单选按钮时触发。我需要它在正常左键单击时触发。
<ListView x:Name="radioListView" SelectionMode="Single" ItemsSource="{Binding}" ItemTemplate="{StaticResource ItemDataTemplate}" SelectionChanged="radioListView_SelectionChanged" Loaded="radioListView_Loaded"></ListView>
似乎找不到任何涉及右键单击强制选择更改事件的内容。标准是左键单击。
预先感谢您:)
编辑:
所以我注意到,当我单击左键单击单选按钮(及其关联文本)的外部时,会触发 SelectionChanged 事件。就好像单选按钮独立于列表视图工作一样。 SelectionChanged 事件在单选按钮元素内右键单击时触发。奇怪,还在想办法。
I have the following XAML/Control Template for a ListViewItem:
<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>
AND when I try to set the SelectionChanged to fire, it ONLY fires when a radio button is checked with a Mouse RIGHT Click. I need it to fire when it is a NORMAL Left Click.
<ListView x:Name="radioListView" SelectionMode="Single" ItemsSource="{Binding}" ItemTemplate="{StaticResource ItemDataTemplate}" SelectionChanged="radioListView_SelectionChanged" Loaded="radioListView_Loaded"></ListView>
Can't seem to find anything referring to forcing a selectionchanged event on a right click. Standard is a left click.
Thank you in advance :)
EDIT:
So I noticed that the SelectionChanged event fires when I Click OUTSIDE of the radio button (and its associated text) on Left Click. It's as if the radio button is working independently from the listview. The SelectionChanged event fires on a Right Click INSIDE the radio button element. Weird, still trying to figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定为什么鼠标左键不起作用,但这是我用来显示带有单选按钮的列表框的样式。它适用于鼠标右键单击和左键单击
它的使用方式就像
如果我必须猜测您的问题,我会猜测常规的 ListView 选择行为是将 LeftMouseClick 事件标记为当您单击某个项目时已处理
I'm not sure why the Left mouse button doesn't work, but here is the style I use for displaying a ListBox with RadioButtons. It works fine with both Right and Left mouse clicks
It is used like
If I had to guess at your problem, I would guess that the regular ListView Selection behavior is marking the LeftMouseClick event as Handled when you click on an item
嗯,那很奇怪!选择是通过右键单击进行的???
我能建议你的是采取一种无事件的方法......
你可以通过绑定 SelectedItem / SelectedValue 属性等来使用 MVVM 模式。
此外,你还可以利用 ListView 的单选模式自动“分组”单选按钮,以便只选择一个有时,
例如单选按钮的
IsChecked
绑定到祖先 ListViewItem 的IsSelected
属性,有两种方式。这样,当选中单选按钮时,它将自动选择列表视图行,并且 SelectedItem\Value 绑定将自动触发。在 ListView 的单选模式下,下一次单击单选按钮将自动取消选择上一行。Well thats strange! Selection takes place on Right click???
All I can suggest you is to take an eventless approach ...
You could use the MVVM pattern by binding SelectedItem / SelectedValue properties etc.
Also you could harness single selection mode of ListView to automatically "group" radio buttons so that ONLY one is selected at time
e.g. radiobutton's
IsChecked
bound to ancestor ListViewItem'sIsSelected
property, two way. This way when a radio button is checked it would automatically select that list view row and SelectedItem\Value binding would fire automatically. And being in single selection mode of ListView, next radio button click would automatically de-select previous row.