Can Poyrazoğlu
Can Poyrazoğlu

Reputation: 34790

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?

Upvotes: 5

Views: 1354

Answers (2)

ardeshir.omid
ardeshir.omid

Reputation: 1

simple Solution for all of these type bugs/errors:

1- bool bFormLoaded;//=false ;


2- at [YourWinOrControlorWPF]_Loaded(object sender, RoutedEventArgs e)


add this flag at end of function:
bFormLoaded=true;

3-at UseDefaultFoldersCB_Checked(...)

add this line
 if(bFormLoaded==false) return;

100%

Upvotes: 0

MaMazav
MaMazav

Reputation: 1884

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.

Upvotes: 5

Related Questions