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 イベントが発生することに気付きました。ラジオボタンがリストビューとは独立して機能しているかのようです。SelectionChanged イベントは、ラジオ ボタン要素内での右クリックで発生します。奇妙なことに、まだそれを理解しようとしています。

4

2 に答える 2

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 イベントを Handled としてマークしていると推測します

于 2011-10-12T18:30:58.603 に答える
0

それは奇妙です!選択は右クリックで行われます???

私があなたに提案できるのは、イベントのないアプローチを取ることだけです...

SelectedItem / SelectedValue プロパティなどをバインドすることで、MVVM パターンを使用できます。

また、ListView の単一選択モードを利用して、ラジオ ボタンを自動的に「グループ化」し、一度に 1 つだけが選択されるようにすることもできます。

たとえば、ラジオボタンIsCheckedは先祖の ListViewItem のIsSelectedプロパティにバインドされ、双方向です。このように、ラジオ ボタンがチェックされると、そのリスト ビューの行が自動的に選択され、SelectedItem\Value バインディングが自動的に起動します。また、ListView の単一選択モードでは、次にラジオ ボタンをクリックすると、前の行が自動的に選択解除されます。

于 2011-10-12T18:16:47.577 に答える