11

XAML にいくつかの RadioButtons があります...

<StackPanel>
    <RadioButton Name="RadioButton1" GroupName="Buttons" Click="ButtonsChecked" IsChecked="True">One</RadioButton>
    <RadioButton Name="RadioButton2" GroupName="Buttons" Click="ButtonsChecked">Two</RadioButton>
    <RadioButton Name="RadioButton3" GroupName="Buttons" Click="ButtonsChecked">Three</RadioButton>
</StackPanel>

そして、Visual Basic コードでクリック イベントを処理できます。これは機能します...

    Private Sub ButtonsChecked(System.Object としての ByVal 送信者、_
                               ByVal e As System.Windows.RoutedEventArgs)
        Select Case CType(sender, RadioButton).Name
            ケース「RadioButton1」
                '何かをする
                終了選択
            ケース「RadioButton2」
                「何かをする 2
                終了選択
            ケース「RadioButton3」
                '何かをする 3
                終了選択
        エンドセレクト
    サブ終了

でも、改善したいです。このコードは失敗します...

<StackPanel>
    <RadioButton Name="RadioButton1" GroupName="Buttons" Click="ButtonsChecked" Command="one" IsChecked="True">One</RadioButton>
    <RadioButton Name="RadioButton2" GroupName="Buttons" Click="ButtonsChecked" Command="two">Two</RadioButton>
    <RadioButton Name="RadioButton3" GroupName="Buttons" Click="ButtonsChecked" Command="three">Three</RadioButton>
</StackPanel>
    Private Sub ButtonsChecked(System.Object としての ByVal 送信者、_
                               ByVal e As System.Windows.RoutedEventArgs)
        Select Case CType(sender, RadioButton).Command
            ケース「一」
                '何かをする
                終了選択
            ケース「2」
                「何かをする 2
                終了選択
            ケース「3」
                '何かをする 3
                終了選択
        エンドセレクト
    サブ終了

私の XAML では、Command=属性に青い波線の下線が表示され、このヒントが表示されます...

'CommandValueSerializer' ValueSerializer は 'System.String' から変換できません。

私のVBでは、ケースの選択行に緑色の波線の下線が表示され、この警告が表示されます...

「System.Windows.Input.ICommand」を「String」に変換するときにランタイム エラーが発生する場合があります。

入力ミスの場合は、実行時エラーではなく、完全な Intellisense とコンパイル エラーで Enum タイプのコマンドを使用することをお勧めします。どうすればこれを改善できますか?

4

2 に答える 2

18

コマンドを機能させるには、xaml またはコード ビハインドでバインディングを設定する必要があります。これらのコマンド バインディングは、以前に宣言された public static フィールドを参照する必要があります。

次に、ボタンの Command 属性で、これらの同じコマンドも参照する必要があります。

<Window 
    x:Class="RadioButtonCommandSample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:RadioButtonCommandSample"
    Title="Window1" 
    Height="300" 
    Width="300"
    >
    <Window.CommandBindings>
        <CommandBinding Command="{x:Static local:Window1.CommandOne}" Executed="CommandBinding_Executed"/>
        <CommandBinding Command="{x:Static local:Window1.CommandTwo}" Executed="CommandBinding_Executed"/>
        <CommandBinding Command="{x:Static local:Window1.CommandThree}" Executed="CommandBinding_Executed"/>
    </Window.CommandBindings>
    <StackPanel>
        <RadioButton Name="RadioButton1" GroupName="Buttons" Command="{x:Static local:Window1.CommandOne}" IsChecked="True">One</RadioButton>
        <RadioButton Name="RadioButton2" GroupName="Buttons" Command="{x:Static local:Window1.CommandTwo}">Two</RadioButton>
        <RadioButton Name="RadioButton3" GroupName="Buttons" Command="{x:Static local:Window1.CommandThree}">Three</RadioButton>
    </StackPanel>
</Window>

public partial class Window1 : Window
{
    public static readonly RoutedCommand CommandOne = new RoutedCommand();
    public static readonly RoutedCommand CommandTwo = new RoutedCommand();
    public static readonly RoutedCommand CommandThree = new RoutedCommand();

    public Window1()
    {
        InitializeComponent();
    }

    private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        if (e.Command == CommandOne)
        {
            MessageBox.Show("CommandOne");
        }
        else if (e.Command == CommandTwo)
        {
            MessageBox.Show("CommandTwo");
        }
        else if (e.Command == CommandThree)
        {
            MessageBox.Show("CommandThree");
        }
    }
}
于 2008-10-31T23:09:42.417 に答える
0

WPF MVVM デザイン パターンを使用したより良いソリューション:

ラジオ ボタン コントロール XAML から Modelview.vb/ModelView.cs へ:

XAML Code:
<RadioButton Content="On" IsEnabled="True" IsChecked="{Binding OnJob}"/>
<RadioButton Content="Off" IsEnabled="True" IsChecked="{Binding OffJob}"/>

ViewModel.vb :

Private _OffJob As Boolean = False
Private _OnJob As Boolean = False

Public Property OnJob As Boolean
    Get
        Return _OnJob
    End Get
    Set(value As Boolean)
        Me._OnJob = value
    End Set
End Property

Public Property OffJob As Boolean
    Get
        Return _OffJob
    End Get
    Set(value As Boolean)
        Me._OffJob = value
    End Set
End Property

Private Sub FindCheckedItem()
  If(Me.OnJob = True)
    MessageBox.show("You have checked On")
 End If
If(Me.OffJob = False)
 MessageBox.Show("You have checked Off")
End sub

上記と同じロジックを使用して、3 つのラジオ ボタンのいずれかをチェックしたかどうかを確認できます。オプション 1、オプション 2、オプション 3。ただし、ブール セット ID が true か false かをチェックすると、ラジオ ボタンがチェックされているかどうかを識別できます。

于 2016-10-06T17:40:21.523 に答える