在选择更改时设置TextBlock可见性

时间:2014-02-27 15:49:40

标签: c# winforms telerik

目前我有一个组合框,想要在某些选择中更改TextBlock的可见性。

错误产生Object reference not set to an instance of an object.

private void Selection(object sender, SelectionChangedEventArgs e)
{
    if (Findpf() == 12)
    {
        DateAutoCompleteBox.Visibility = System.Windows.Visibility.Collapsed;
    }
    else
    {
        DateAutoCompleteBox.Visibility = System.Windows.Visibility.Visible;
    }
}

尝试了= Visibility.Collapsed同样的结果。我该如何解决这个问题?

public uint Findpf()
{
    if (Pf.Text == "Annual")
    {
        return 1;
    }
    if (Pf.Text == "Semi-annual")
    {
        return 2;
    }
    if (Pf.Text == "Tri-Annual")
    {
        return 3;
    }
    if (Pf.Text == "Quarterly")
    {
        return 4;
    }
   if (Pf.Text == "Bi-Monthly")
    {
        return 6;
    }
     if (Pf.Text == "Monthly")
    {
         return 12;
    }
}

Autocompletebox Xaml的初始化

<telerik:RadAutoCompleteBox x:Name="DateAutoCompleteBox" Visibility="Visible" Width="220"></telerik:RadAutoCompleteBox>

2 个答案:

答案 0 :(得分:1)

你可以尝试这个来检查什么值是null

private void Selection(object sender, SelectionChangedEventArgs e)
{
    if(DateAutoCompleteBox == null)
    {
        MessageBox.Show("DateAutoCompleteBox   is null"); return;
    }
    if(Pf == null)
    {
        MessageBox.Show("Pf  is null"); return;
    }
    if(Pf.Text == null)
    {
        MessageBox.Show("Pf.Text  is null"); return;
    }
    if (Findpf() == 12)
    {
        DateAutoCompleteBox.Visibility = System.Windows.Visibility.Collapsed;
    }
    else
    {
        DateAutoCompleteBox.Visibility = System.Windows.Visibility.Visible;
    }
}

答案 1 :(得分:0)

DateAutoCompleteBox设置为null,您无法访问或设置未初始化的对象的属性。