简体   繁体   English

带有RadioButtons的ListView仅在右键单击时需要激发/提高SelectionChanged(需要左键单击)

[英]ListView with RadioButtons fires/raises SelectionChanged ONLY on Right-Click (Need left-click)

I have the following XAML/Control Template for a ListViewItem: 我为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>

AND when I try to set the SelectionChanged to fire, it ONLY fires when a radio button is checked with a Mouse RIGHT Click. 而且,当我尝试将SelectionChanged设置为触发时,仅当用鼠标右键单击选中单选按钮时才会触发。 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. 因此,我注意到,当我单击“左键单击”时单选按钮的“外部”(及其相关文本)时,将触发SelectionChanged事件。 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. 右键单击单选按钮元素内的SelectionChanged事件。 Weird, still trying to figure it out. 很奇怪,仍在设法弄清楚。

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. 您可以通过绑定SelectedItem / SelectedValue属性等来使用MVVM模式。

Also you could harness single selection mode of ListView to automatically "group" radio buttons so that ONLY one is selected at time 您还可以利用ListView的单一选择模式来自动“分组”单选按钮,以便一次只能选择一个

eg radiobutton's IsChecked bound to ancestor ListViewItem's IsSelected property, two way. 例如, IsSelected按钮的IsChecked绑定到祖先ListViewItem的IsSelected属性,有两种方式。 This way when a radio button is checked it would automatically select that list view row and SelectedItem\\Value binding would fire automatically. 这样,当选中单选按钮时,它将自动选择列表视图行,并且SelectedItem \\ Value绑定将自动触发。 And being in single selection mode of ListView, next radio button click would automatically de-select previous row. 在ListView的单选模式下,单击单选按钮将自动取消选择上一行。

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. 我不确定为什么鼠标左键不起作用,但是这是我用来显示带有RadioButtons的ListBox的样式。 It works fine with both Right and Left mouse clicks 右键单击和左击都可以正常工作

<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>

It is used like 它像

<ListBox ItemsSource="{Binding MyItems}" 
         Style="{StaticResource ListBoxAsRadioButtonsStyle}" />

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 如果我不得不猜测您的问题,则可能会认为常规的ListView选择行为是在您单击某项时将LeftMouseClick事件标记为Handled。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM