1

I've got a form that gets given a datarow from a dataset to bind all its elements. One of them is a bool, but I want that bool to be represented by by a Yes/No combo box. So I did this and it works nicely.

I also want to bind the visibility of a couple elements to this bool field. When the form loads, the initial setting of the visibility works. When I change the combobox selection, the ConvertBack() method of the ComboBox gets called (i.e. it's setting the bound value). But the other elements that have their visibility bound to that same field don't get updated. I set breakpoints in the Conversion methods and they never get called like they do when the form loads.

Here's the relevant XAML:

<ComboBox SelectedIndex="{Binding Path=[Adequate], Converter={StaticResource b2iConverter}}" Name="cb_Adequate" >
     <ComboBoxItem>Yes</ComboBoxItem>
     <ComboBoxItem>No</ComboBoxItem>
</ComboBox>

<Label Content="Reason:" 
       VerticalAlignment="Center" 
       Visibility="{Binding Path=[Adequate], 
       Converter={StaticResource b2vConverterInverse}}"/>

<TextBox Text="{Binding Path=[NotAdequateReason]}" 
         Visibility="{Binding Path=[Adequate], 
          Converter={StaticResource b2vConverterInverse}}"/>
  • "Adequate" is the bool field
  • b2iConverter is just booleanToIndexConverter (from the above link)
  • b2vConverterInverse is just an inverted boolean to visibility converter (I want the label and textbox shown when Adequate is FALSE or 0).

Thanks for any help. I can post more code if needed, I figure the problem is in the XAML...

EDIT: Apparently it's not possible with XAML (see Greg's post below), so I just do it in code:

private void cb_Adequate_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
     Visibility vis = (cb_Adequate.SelectedItem as ComboBoxItem).Content.ToString() == "Yes" ? Visibility.Collapsed : Visibility.Visible;
     label_Reason.Visibility = tb_AdequateDesc.Visibility = vis;
}
Community
  • 1
  • 1
akevan
  • 691
  • 1
  • 9
  • 21
  • Have you tried explicitly setting the binding mode? You might also try removing the square brackets `[]` around the `Path`, I don't think that is needed here. – WildCrustacean Dec 18 '12 at 02:46

1 Answers1

1

If you want your UI elements to change state when a data property changes, you need to implement INotifyPropertyChanged on your data class.

This means that you can't use the DataRow for your purposes. You'll have to create a new class, then at run time populate it with values from the DataRow and then bind that object to your view.

Greg Sansom
  • 20,442
  • 6
  • 58
  • 76
  • Or just do it in code behind? Seems easier. I was looking for a XAML solution. – akevan Dec 18 '12 at 05:40
  • There isn't a XAML only solution, as XAML properties only update automatically when a NotifyPropertyChanged event fires. This doesn't happen with a DataRow. You could do it in code-behind but data binding is neater and recommended for WPF/XAML solutions. – Greg Sansom Dec 18 '12 at 06:43
  • OK, if it's not possible with XAML, then isn't it easier just to implement the OnSelectionChanged of the combobox and set the visibility there? Seems like less code... – akevan Dec 18 '12 at 17:17