1

我有以下用于 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 事件在单选按钮元素内单击鼠标右键时触发。奇怪,还在想办法。

4

2 回答 2

0

那很奇怪!选择发生在右键单击???

我只能建议你采取一种无事的方法......

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

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

例如,radiobuttonIsChecked绑定到祖先 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. 并且在 ListView 的单选模式下,下一个单选按钮单击将自动取消选择上一行。

于 2011-10-12T18:16:47.577 回答
0

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

于 2011-10-12T18:30:58.603 回答