Dot-Net

如何最好地處理 WPF 單選按鈕?

  • October 6, 2016

我的 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(ByVal sender As System.Object, _
ByVal e As System.Windows.RoutedEventArgs)
選擇案例 CType(sender, RadioButton).Name
案例“RadioButton1”
'做某事
退出選擇
案例“RadioButton2”
'做兩件事
退出選擇
案例“RadioButton3”
'做三件事
退出選擇
結束選擇
結束子

但是,我想改進它。此程式碼失敗…

<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(ByVal sender As System.Object, _
ByVal e As System.Windows.RoutedEventArgs)
選擇案例 CType(sender, RadioButton).Command
案例“一”
'做某事
退出選擇
案例“二”
'做兩件事
退出選擇
案例“三”
'做三件事
退出選擇
結束選擇
結束子

在我的 XAML 中,我在**Command=**屬性上得到一個藍色波浪下劃線,並且這個提示……

“CommandValueSerializer”ValueSerializer 無法從“System.String”轉換。

在我的 VB 中,我在Select Case行上得到一個綠色波浪下劃線,並且這個警告……

將“System.Windows.Input.ICommand”轉換為“String”時可能會出現執行時錯誤。

更好的是使用帶有完整 Intellisense 的 Enum 類型命令並編譯錯誤而不是執行時錯誤,以防出現拼寫錯誤。我該如何改進呢?

為了使命令起作用,您需要在您的 xaml 或程式碼後面設置綁定。這些命令綁定必須引用先前已聲明的公共靜態欄位。

然後在您的按鈕命令屬性中,您還需要引用這些相同的命令。

<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");
       }
   }
}

引用自:https://stackoverflow.com/questions/254992