繁体   English   中英

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

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

我为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事件。 很奇怪,仍在设法弄清楚。

那真是奇怪! 选择发生在右键单击上???

我能建议您采取的是无懈可击的方法...

您可以通过绑定SelectedItem / SelectedValue属性等来使用MVVM模式。

您还可以利用ListView的单一选择模式来自动“分组”单选按钮,以便一次只能选择一个

例如, IsSelected按钮的IsChecked绑定到祖先ListViewItem的IsSelected属性,有两种方式。 这样,当选中单选按钮时,它将自动选择列表视图行,并且SelectedItem \\ Value绑定将自动触发。 在ListView的单选模式下,单击单选按钮将自动取消选择上一行。

我不确定为什么鼠标左键不起作用,但是这是我用来显示带有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事件标记为Handled。

暂无
暂无

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

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