wpf RadioButton 被选中,它的 Checked 事件在运行应用程序时被调用,并报告异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16391546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RadioButton is checked, it's Checked event is call when run the app, and report exception
提问by SubmarineX
There are two RadioButtons and two Labels in .xaml, and check one RadioButtonmake the corresponding LabelEnable. RadioButton1is checked . Run the app, and the Checked Eventis called automatically, but Label is null, so the app report exception.
中有两个RadioButtons和两个Labels .xaml,勾选一个RadioButton使对应的LabelEnable。RadioButton1被检查。运行app,Checked Event自动调用了,但是Label为null,所以app报异常。
I just use a variable to mark whether the window is first created. Whether there are other methods? I don't know why the app run the events after initialize all components, who can tell me?
我只是用一个变量来标记窗口是否是第一次创建的。是否还有其他方法?我不知道为什么应用程序在初始化所有组件后运行事件,谁能告诉我?
MainWindow.xaml
主窗口.xaml
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="350"
Width="525">
<StackPanel>
<RadioButton
GroupName="Group"
IsChecked="True"
Content="RadioButton1"
x:Name="RadioButton1"
Checked="RadioButton1_Checked"
Unchecked="RadioButton1_Unchecked"/>
<RadioButton
GroupName="Group"
Content="RadioButton2"
x:Name="RadioButton2"
Checked="RadioButton2_Checked"
Unchecked="RadioButton2_Unchecked"/>
<Label
x:Name="Label1"
Content="Label1"/>
<Label
IsEnabled="False"
x:Name="Label2"
Content="Label2"/>
</StackPanel>
</Window>
MainWindow.xaml.cs
主窗口.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication7
{
/// <summary>
/// MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private bool isFirstRun;
public MainWindow()
{
isFirstRun = true;
InitializeComponent();
isFirstRun = false;
}
private void RadioButton1_Checked(object sender, RoutedEventArgs e)
{
if (isFirstRun)
return;
Label1.IsEnabled = true;
}
private void RadioButton2_Checked(object sender, RoutedEventArgs e)
{
Label1.IsEnabled = false;
}
private void RadioButton1_Unchecked(object sender, RoutedEventArgs e)
{
Label2.IsEnabled = true;
}
private void RadioButton2_Unchecked(object sender, RoutedEventArgs e)
{
Label2.IsEnabled = false;
}
}
}
回答by sa_ddam213
I'm not sure if you have a specific reason for the EventHandlers, but if not you can do this logic directly in the Xamlusing ElementNamebinding
我不确定您是否有特定的原因EventHandlers,但如果没有,您可以直接在XamlusingElementName绑定中执行此逻辑
Example:
例子:
<StackPanel>
<RadioButton x:Name="RadioButton1"
GroupName="Group"
IsChecked="True"
Content="RadioButton1" />
<RadioButton x:Name="RadioButton2"
GroupName="Group"
Content="RadioButton2" />
<Label x:Name="Label1"
IsEnabled="{Binding IsChecked, ElementName=RadioButton1}"
Content="Label1"/>
<Label x:Name="Label2"
IsEnabled="{Binding IsChecked, ElementName=RadioButton2}"
Content="Label2"/>
</StackPanel>
回答by Paul Marsland
Why not simply remove the Ischecked="True"from the XAML and replace the isFirstRun = falsevariable being set after InitializeComponent();with RadioButton1.IsChecked = true
为什么不直接删除Ischecked="True"从XAML和更换isFirstRun = false被设置后,变量InitializeComponent();与RadioButton1.IsChecked = true
this way you save having to define the isFirstRunvariable.
这样你就不必定义isFirstRun变量了。

