I'm new in WPF and I Want to hide/show some controls like TextBlock, ComboBox etc from code behind depending of a ComboBox Value. I've searched for some solutions without luck. I often the follow answer.
textbox1.Visibility = Visibility.Hidden;
So, I tried this one.
private void cbBuscar_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show(cbBuscar.SelectedIndex.ToString());
if (cbBuscar.SelectedIndex == 0)
{
cbProduto.Visibility = Visibility.Hidden;
}
else if (cbBuscar.SelectedIndex == 1)
{
cbProduto.Visibility = Visibility.Visible;
}
else if (cbBuscar.SelectedIndex == 2)
{
cbProduto.Visibility = Visibility.Collapsed;
}
}
It simple does not work. Trying that I get this error {"Object reference not set to an instance of an object."}
Do what I'm trying must not be hard, actually it must be pretty easy. Then, could anyone tell what I'm doing wrong?
Try placing the following code inside your cbBuscar_SelectionChanged
function:
if (!IsLoaded)
return;
If the selection is changing before the window initializes, this may fix the problem.
So your function would look like this:
private void cbBuscar_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!IsLoaded)
return;
MessageBox.Show(cbBuscar.SelectedIndex.ToString());
if (cbBuscar.SelectedIndex == 0)
{
cbProduto.Visibility = Visibility.Hidden;
}
else if (cbBuscar.SelectedIndex == 1)
{
cbProduto.Visibility = Visibility.Visible;
}
else if (cbBuscar.SelectedIndex == 2)
{
cbProduto.Visibility = Visibility.Collapsed;
}
}
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.