简体   繁体   中英

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?

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.

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%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM