如何在 IF 语句中使用单选按钮?
How to use a radiobutton with IF statement?
我正在尝试将 RadioButton 与语句 IF
一起使用
简而言之:我创建了一个 WPF 应用程序,我想在其中 select 四个选项之一,然后单击按钮打开另一个 window.I 希望确保用户单击一次。
private void Button_Click(object sender, RoutedEventArgs e)
{
if (here I don't know how to create a condition)
{
MessageBox.Show("Musíš zvolit, jednu z početních operací!");
}
else
{
uroven lvl = new uroven();
lvl.Show();
this.Close();
}
}
RadioButton == unchecked 我想到了这个条件,但它没有 or(||) 的可能性那么好
从代码中可以看出,我在 if 和 else 中使用错误消息打开另一个 window
我很乐意提供任何建议
谢谢
编辑:
解决方案虽然不是很好
private void Button_Click(object sender, RoutedEventArgs e)
{
if (scitani.IsChecked.HasValue && scitani.IsChecked.Value)
{
uroven lvl = new uroven();
lvl.Show();
this.Close();
}
else if (odcitani.IsChecked.HasValue && odcitani.IsChecked.Value)
{
uroven lvl = new uroven();
lvl.Show();
this.Close();
}
else if (nasobeni.IsChecked.HasValue && nasobeni.IsChecked.Value)
{
uroven lvl = new uroven();
lvl.Show();
this.Close();
}
else if (deleni.IsChecked.HasValue && deleni.IsChecked.Value)
{
uroven lvl = new uroven();
lvl.Show();
this.Close();
}
else
{
MessageBox.Show("Musíš zvolit, jednu z početních operací!");
}
}
你应该达到你的控制
XAML:
<RadioButton x:Name="myRadioButton" />
CS:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (myRadioButton.IsChecked.HasValue && myRadioButton.IsChecked.Value)
{
MessageBox.Show("Musíš zvolit, jednu z početních operací!");
}
else
{
uroven lvl = new uroven();
lvl.Show();
this.Close();
}
}
我使用 MVVM 模式为您制作了示例源。
结构
首先,
你最好使用 ListBox,其中包含 RadioButton
作为 ListBoxItem。
MainMenuViewModel.cs
public class MainMenuViewModel : ObservableObject
{
private List<RadioItem> _radios;
public List<RadioItem> Radios
{
get { return _radios; }
set { _radios = value; OnPropertyChanged(); }
}
private RadioItem _currentRadio;
public RadioItem CurrentRadio
{
get { return _currentRadio; }
set { _currentRadio = value; OnPropertyChanged(); }
}
...
}
MainMenuResource.xaml
<Style TargetType="{x:Type ListBox}" x:Key="LBX">
<Setter Property="ItemContainerStyle" Value="{StaticResource LBXI}"/>
<Setter Property="ItemsSource" Value="{Binding Radios}"/>
<Setter Property="SelectedItem" Value="{Binding CurrentRadio}"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="BorderBrush" Value="Transparent"/>
</Style>
Radios
绑定到ListBox中的ItemsSource,CurrnetRadio
绑定到SelectedItem。
第二,
通过使用Command
,您可以关闭主窗口并显示子窗口。
MainMenuViewModel.cs
public class MainMenuViewModel : ObservableObject
{
...
public ICommand ClickCommand { get; set; }
public MainMenuViewModel()
{
ClickCommand = new RelayCommand<Window>(Click);
...
}
private void Click(Window obj)
{
if (CurrentRadio.DisplayName == "Radio 1")
{
obj.DialogResult = true;
}
else
{
MessageBox.Show("Select another one!");
}
}
}
MainMenuResource.xaml
<Style TargetType="{x:Type Button}" x:Key="BTN.MAIN">
<Setter Property="Command" Value="{Binding ClickCommand}"/>
<Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource AncestorType=Window}}"/>
...
</Style>
最后,
因为在应用中需要控制多个windows,所以一般用OnStartup方法处理
App.xaml.cs
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow main;
SubWindow sub;
this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
main = new MainWindow();
main.ShowDialog();
if (main.DialogResult == true)
{
sub = new SubWindow();
sub.ShowDialog();
}
Environment.Exit(0);
}
}
我正在尝试将 RadioButton 与语句 IF
一起使用简而言之:我创建了一个 WPF 应用程序,我想在其中 select 四个选项之一,然后单击按钮打开另一个 window.I 希望确保用户单击一次。
private void Button_Click(object sender, RoutedEventArgs e)
{
if (here I don't know how to create a condition)
{
MessageBox.Show("Musíš zvolit, jednu z početních operací!");
}
else
{
uroven lvl = new uroven();
lvl.Show();
this.Close();
}
}
RadioButton == unchecked 我想到了这个条件,但它没有 or(||) 的可能性那么好
从代码中可以看出,我在 if 和 else 中使用错误消息打开另一个 window
我很乐意提供任何建议
谢谢
编辑: 解决方案虽然不是很好
private void Button_Click(object sender, RoutedEventArgs e)
{
if (scitani.IsChecked.HasValue && scitani.IsChecked.Value)
{
uroven lvl = new uroven();
lvl.Show();
this.Close();
}
else if (odcitani.IsChecked.HasValue && odcitani.IsChecked.Value)
{
uroven lvl = new uroven();
lvl.Show();
this.Close();
}
else if (nasobeni.IsChecked.HasValue && nasobeni.IsChecked.Value)
{
uroven lvl = new uroven();
lvl.Show();
this.Close();
}
else if (deleni.IsChecked.HasValue && deleni.IsChecked.Value)
{
uroven lvl = new uroven();
lvl.Show();
this.Close();
}
else
{
MessageBox.Show("Musíš zvolit, jednu z početních operací!");
}
}
你应该达到你的控制
XAML:
<RadioButton x:Name="myRadioButton" />
CS:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (myRadioButton.IsChecked.HasValue && myRadioButton.IsChecked.Value)
{
MessageBox.Show("Musíš zvolit, jednu z početních operací!");
}
else
{
uroven lvl = new uroven();
lvl.Show();
this.Close();
}
}
我使用 MVVM 模式为您制作了示例源。
结构
首先,
你最好使用 ListBox,其中包含 RadioButton
作为 ListBoxItem。
MainMenuViewModel.cs
public class MainMenuViewModel : ObservableObject
{
private List<RadioItem> _radios;
public List<RadioItem> Radios
{
get { return _radios; }
set { _radios = value; OnPropertyChanged(); }
}
private RadioItem _currentRadio;
public RadioItem CurrentRadio
{
get { return _currentRadio; }
set { _currentRadio = value; OnPropertyChanged(); }
}
...
}
MainMenuResource.xaml
<Style TargetType="{x:Type ListBox}" x:Key="LBX">
<Setter Property="ItemContainerStyle" Value="{StaticResource LBXI}"/>
<Setter Property="ItemsSource" Value="{Binding Radios}"/>
<Setter Property="SelectedItem" Value="{Binding CurrentRadio}"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="BorderBrush" Value="Transparent"/>
</Style>
Radios
绑定到ListBox中的ItemsSource,CurrnetRadio
绑定到SelectedItem。
第二,
通过使用Command
,您可以关闭主窗口并显示子窗口。
MainMenuViewModel.cs
public class MainMenuViewModel : ObservableObject
{
...
public ICommand ClickCommand { get; set; }
public MainMenuViewModel()
{
ClickCommand = new RelayCommand<Window>(Click);
...
}
private void Click(Window obj)
{
if (CurrentRadio.DisplayName == "Radio 1")
{
obj.DialogResult = true;
}
else
{
MessageBox.Show("Select another one!");
}
}
}
MainMenuResource.xaml
<Style TargetType="{x:Type Button}" x:Key="BTN.MAIN">
<Setter Property="Command" Value="{Binding ClickCommand}"/>
<Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource AncestorType=Window}}"/>
...
</Style>
最后,
因为在应用中需要控制多个windows,所以一般用OnStartup方法处理
App.xaml.cs
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow main;
SubWindow sub;
this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
main = new MainWindow();
main.ShowDialog();
if (main.DialogResult == true)
{
sub = new SubWindow();
sub.ShowDialog();
}
Environment.Exit(0);
}
}