简体   繁体   English

单选按钮已启用故障

[英]Radio Button Isenabled trouble

I'm setting a combobox to be enabled based on selection of Radioboxes. 我正在根据对单选框的选择设置要启用的组合框。 Currently it's producing this error Object reference not set to an instance of an object. 当前,它正在产生此错误Object reference not set to an instance of an object. for the following code below. 用于以下代码。 I did it one at a time and to make the ComboBox False works. 我一次做了一个,使ComboBox False起作用。 When I implemented Disc_OnChecked to Set to true it produced the error. 当我将Disc_OnChecked设置为true时,会产生错误。 If I can get help to bypass this error please. 如果我可以帮助绕过此错误,请。

private void Cont_OnChecked(object sender, RoutedEventArgs e)
{
    Cf.IsEnabled = false;

}

private void Disc_OnChecked(object sender, RoutedEventArgs e)
{
    Cf.IsEnabled = true;
}

Xaml code: XAML代码:

<GroupBox>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Type:     "></TextBlock>
        <RadioButton Checked="Disc_OnChecked" GroupName="Group1" x:Name="Disc" IsChecked="true"  Content="Discrete" ></RadioButton>
        <RadioButton Checked="Cont_OnChecked" GroupName="Group1" x:Name="Cont" Content="Continuous"></RadioButton>
    </StackPanel>
</GroupBox>

<ComboBox x:Name="Cf" Width="125" SelectedIndex="1">
    <ComboBoxItem Content="Annual"></ComboBoxItem>
    <ComboBoxItem Content="Semi-annual"></ComboBoxItem>
</ComboBox>

Let me take a stab at it. 让我刺一下。

This is probably hapenning when your controls are getting wired up, and at this point the ComboBox has not been instantiated. 当控件连接起来时,这可能正在暂停,此时ComboBox尚未实例化。

Simply check for null within your handlers: 只需在处理程序中检查null:

private void Cont_OnChecked(object sender, RoutedEventArgs e)
{
    if (Cf != null)
       Cf.IsEnabled = false;

}

private void Disc_OnChecked(object sender, RoutedEventArgs e)
{
    if (Cf != null)
       Cf.IsEnabled = true;
}

Cheers 干杯

You can also bind the ComboBoxItem IsSelected property to the RadioButton IsChecked property 您还可以将ComboBoxItem IsSelected属性绑定到RadioButton IsChecked属性。

<StackPanel>
  <RadioButton x:Name="rbtnA" Content="A"/>
  <RadioButton x:Name="rbtnB" Content="B"/>
  <RadioButton x:Name="rbtnC" Content="C"/>
  <ComboBox>
      <ComboBoxItem Content="ComboBoxItem A" IsSelected="{Binding ElementName=rbtnA,Path=IsChecked}"/>
      <ComboBoxItem Content="ComboBoxItem B" IsSelected="{Binding ElementName=rbtnB,Path=IsChecked}"/>
      <ComboBoxItem Content="ComboBoxItem C" IsSelected="{Binding ElementName=rbtnC,Path=IsChecked}"/>
  </ComboBox>
</StackPanel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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