Search code examples
c#wpfxaml

WPF checkbox Tag is null eventhough it is set in XAML


I am trying to make use of the Tag property of the WPF checkbox. This is my XAML:

<CheckBox Content="BTC/USD" HorizontalAlignment="Left" Margin="14,0,0,41" 
VerticalAlignment="Bottom" IsChecked="True" Checked="CheckBox_Checked" Tag="btcusd" 
 Unchecked="CheckBox_Checked"/>

When I open my app, CheckBox_Checked is called immediately, but the sender's Tag property is null. Why can this happen?


Solution

  • The Checked property is set right on the XAML loading, when you set IsChecked="True". The tag may be loaded only later when the XAML loading code decides to set this property. That's why you can see uninitialized properties.