尝试为我的单选按钮设置默认值,但会标记为NullReferenceException错误。
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是应该检查的默认单选按钮。
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" />
如果我没有将rbImperial Checked属性设为True以使其默认被选中,那么它运行得非常好。
我错过了什么?
当我尝试运行它时出现问题。
答案 0 :(得分:0)
尝试在构造函数中设置默认值。不是最漂亮的方法,但它应该工作。或者,您可以将其绑定到ViewModel;这应该也可以。