wpf - RadioButton is checked, it's Checked event is call when run the app, and report exception -


there 2 radiobuttons , 2 labels in .xaml, , check 1 radiobutton make corresponding label enable. radiobutton1 checked . run app, , checked event called automatically, label null, app report exception.

i use variable mark whether window first created. whether there other methods? don't know why app run events after initialize components, can tell me?

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

i'm not sure if have specific reason eventhandlers, if not can logic directly in xaml using elementname binding

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> 

Comments