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