本文介绍了当我使用comboBoxCities.Visibility = System.Windows.Visibility.Hidden时,为什么程序会崩溃;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有一个组合框,我有一个名为  comboBoxCountry_SelectionChanged的事件,当我将以下代码添加到程序崩溃时,当它试图启动时" comboBoxCities.Visibility = System.Windows.Visibility.Hidden;"

I have a combobox and I have an event for it called comboBoxCountry_SelectionChanged when I add the following code to event the program crashes when its trying to start "comboBoxCities.Visibility = System.Windows.Visibility.Hidden;"

我已粘贴下面的c#和xaml代码

I have pasted the c# and xaml code below

 

C#代码

 


  private void comboBoxCountry_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
   comboBoxCities.Visibility = System.Windows.Visibility.Hidden;
   labelCity.Visibility = System.Windows.Visibility.Hidden;

   if (comboBoxCountry.SelectedItem.ToString().Contains("United States"))
   {
    comboBoxCities.Visibility = System.Windows.Visibility.Visible;
    labelCity.Visibility = System.Windows.Visibility.Visible;
   }

   else if (comboBoxCountry.SelectedItem.ToString().Contains("Canada"))
   {
    comboBoxCities.Visibility = System.Windows.Visibility.Visible;
    labelCity.Visibility = System.Windows.Visibility.Visible;
   }
  }

推荐答案


private void comboBoxCountry_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  if (comboBoxCities == null)
    return;

  comboBoxCities.Visibility = System.Windows.Visibility.Hidden;
  labelCity.Visibility = System.Windows.Visibility.Hidden;

  if (comboBoxCountry.SelectedItem.ToString().Contains("United States"))
  {
    comboBoxCities.Visibility = System.Windows.Visibility.Visible;
    labelCity.Visibility = System.Windows.Visibility.Visible;
  }

  else if (comboBoxCountry.SelectedItem.ToString().Contains("Canada"))
  {
    comboBoxCities.Visibility = System.Windows.Visibility.Visible;
    labelCity.Visibility = System.Windows.Visibility.Visible;
  }
}


这篇关于当我使用comboBoxCities.Visibility = System.Windows.Visibility.Hidden时,为什么程序会崩溃;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 18:13