简体   繁体   中英

Setting TextBlock Visibility on selection Change

Currently I have a combo box, and want to change the visibility of a TextBlock on certain selections.

Error it's producing 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;
    }
}

Tried = Visibility.Collapsed also and same result. How do i fix this ?

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;
    }
}

Initialization of Autocompletebox Xaml

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

you can try this to check what value is 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;
    }
}

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

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