Accessing parent-level controls from inside a ComboBox's child controls
- by eponymous23
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 listbox but the textbox controls are presumably out of the SelectionChanged event's scope. Do I have to programmatically traverse the control hierarchy and find the controls?