RadioButton aktiviert ist, wird Geprüft, event aufrufen, wenn Sie die app ausführen, und Bericht Ausnahme

Gibt es zwei RadioButtons und zwei Labels in .xaml, und aktivieren Sie eine RadioButton machen Sie die entsprechenden Label Ermöglichen. RadioButton1 aktiviert ist . Führen Sie die app aus, und die Checked Event wird automatisch aufgerufen, aber das Label null ist, so ist die app report Ausnahme.

Ich nur eine variable zu verwenden, um zu markieren, ob die Fenster zum ersten mal erstellt. Ob es andere Methoden gibt? Ich weiß nicht, warum die app laufen die Ereignisse nach dem initialisieren aller Komponenten, wer kann mir sagen?

MainWindow.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

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;
        }
    }
}
InformationsquelleAutor SubmarineX | 2013-05-06
Schreibe einen Kommentar