views:

42

answers:

1

I have XAML similar to this:

<ListBox ItemsSource="{Binding SearchCriteria, Source={StaticResource model}}" SelectionChanged="cboSearchCriterionType_SelectionChanged">
<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Name="spCriterion" Orientation="Horizontal" Height="20">
            <ComboBox Name="cboSearchCriterionType" Width="120" SelectionChanged="cboSearchCriterionType_SelectionChanged">
                <ComboBox.Items>
                    <ComboBoxItem IsSelected="True" Content="Anagram Match" />
                    <ComboBoxItem Content="Pattern Match" />
                    <ComboBoxItem Content="Subanagram Match" />
                    <ComboBoxItem Content="Length" />
                    <ComboBoxItem Content="Number of Vowels" />
                    <ComboBoxItem Content="Number of Anagrams" />
                    <ComboBoxItem Content="Number of Unique Letters" />
                </ComboBox.Items>
            </ComboBox>
            <TextBox x:Name="SearchSpec" Text="{Binding SearchSpec}" />
            <TextBox x:Name="MinValue" Text="{Binding MinValue}" Visibility="Collapsed" />
            <TextBox x:Name="MaxValue" Text="{Binding MaxValue}" Visibility="Collapsed" />
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

As you can tell from the markup, I have a listbox that is bound to a collection of SearchCriterion objects (collectively contained in a SearchCriteria object). The idea is that the user can add/remove criterion items from the criteria, each criterion is represented by a listbox item. Inside the listbox item I have a combobox and three textboxes. What I'm trying to do is change the visibility of the TextBox controls depending on the item that is selected in the ComboBox. For example, if the user selects "Pattern Match" then I want to show only the first textbox and hide the latter two; conversely, if the user selects "Length" or any of the "Number of..." items, then I want to hide the first TextBox and show the latter two.

What is the best way to achieve this? I was hoping to do something simple in the SelectionChanged event handler for the combobox but the textbox controls are presumably out of the combobox's SelectionChanged event scope. Do I have to programmatically traverse the control hierarchy and find the controls?

A: 

You could bind the visibility to the combobox selected item and then use a value converter to return the visibility. here is a demo of how to do Binding Converters that might help you.

<TextBox x:Name="MinValue" Text="{Binding MinValue}" Visibility="{Binding SelectedItem, ElementName=cboSearchCriterionType, Converter={StaticResource MyConverter}}" />

// declare the converter in xaml....
<SomeElement.Resource>
<local:MyConverter x:Key="MyConverter"/>
</SomeElement.Resource>

public class MyConverter : IValueConverter
{
   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
   {
      // value will be the combobox.selecteditem
      // do stuff and return the visibility
   }
}
Muad'Dib
Thanks, this worked. I had to create two ValueConverter classes: one for the search spec textbox, and one for the min/max textboxes. I didn't see a way to create one ValueConverter class for all three textboxes because I couldn't see a way for the Convert method to know who was invoking it.
eponymous23