简体   繁体   中英

Radio buttons in XAML

Trying to have a default value for my radio buttons, but get flagged an NullReferenceException error.

    private void rbImperial_Checked(object sender, RoutedEventArgs e)
    {
            txtInches.Visibility = Visibility.Visible;
            lblInches.Visibility = Visibility.Visible;
            lblWeight.Text = "lbs";
            lblHeight.Text = "Feet";
    }

    private void rbMetric_Checked(object sender, RoutedEventArgs e)
    {
        lblHeight.Text = "cm";
        txtInches.Visibility = Visibility.Collapsed;
        lblInches.Visibility = Visibility.Collapsed;
        lblWeight.Text = "kg";
    }

rbImperial is the defaulted radio button that should be checked.

XAML:

<RadioButton x:Name="rbMetric" Content="Metric" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="132,244,0,0" Checked="rbMetric_Checked" ClickMode="Press"/>
<RadioButton x:Name="rbImperial" Content="Imperial" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="132,199,0,0" Checked="rbImperial_Checked" IsChecked="True" ClickMode="Press" />

If I don't have the rbImperial Checked property to be True to make it defaulted to be selected it runs perfectly fine.

What am I missing?

My issue when I try to run it.

在此处输入图片说明

Try setting the default value in the constructor. Not the prettiest method, but it should work. Alternatively, you can bind it to the ViewModel; that should work as well.

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