Search code examples
c#wpfuser-controls

How to make a user content hidden from another user control?


I have a hierarchical form of WPF page like this:

-> User Control 1

-> User Control 2 -> User control 3 or User Control 4 or User Control 5

I've in User Control 2 tab controls and tab item , the content of the tab item in User Control 2 is User Control 3.

User Control 1 contains a Combobox contains three states different, so when the user clicks on this Combobox ( event selection_changed ) the content of the tab item in User control 2 will change depending on what the user chooses in the Combobox and get User Control 3 or User Control 4 or User Control 5.

I would like to make the content of the tab item changes every time the user chooses a different ComboBox item.

My approach is to make the user control called in the tab item Hidden and change it depending on the selection_changed event.

What I need exactly now, is how can I make the content of the tab item ( UC1 or UC2, or UC3) in user control 2 visible from the Page when I changed the Combobox that existed in User Control 1.

UC_2.xaml

   <TabItem x:Name="Tabitem1" Grid.Row="2"   Height="auto" Width="auto" >
                <Border BorderBrush="Black" Background="White" BorderThickness="0.8" DockPanel.Dock="Top">

                    <DockPanel Visibility="Hidden" x:Name="dockpanel1" Width="auto" Height="auto">
                     <!--   <local1:UC_3 x:Name="UC3" />-->
                        <Frame  x:Name="frame1"  Width="auto" Height="auto" Source="UC3.xaml"></Frame>

                    </DockPanel>
                </Border>

UC1.xaml.cs

  private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (Comboboxworks.SelectedItem.ToString() == "test1")
        {
            MessageBox.Show("Ok");
        }
        else if (Comboboxworks.SelectedItem.ToString() == "test2")
        {
            ucsecond.dockpanel1.Visibility = Visibility.Visible;

            MessageBox.Show("Yes");
        }
    }

Update :

Page.xaml

 <StackPanel Grid.Row="1" Width="auto" Height="auto" >
            <DockPanel Width="1000" Height="400" HorizontalAlignment="Right">
                <local1:UC2 Width="1002" Height="400" VerticalAlignment="Top"/>
            </DockPanel>
        </StackPanel>

Solution

  • You could use the following helper method to get a reference to the parent Page from the UserControl:

    private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
    {
        var parent = VisualTreeHelper.GetParent(dependencyObject);
        if (parent == null)
            return null;
    
        var parentT = parent as T;
        return parentT ?? FindParent<T>(parent);
    }
    

    Sample usage:

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Page1 parentPage = FindParent<Page1>(this);
        if (parentPage != null)
        {
            parentPage.ucsecond.dockpanel1.Visibility = Visibility.Visible;
            ...
        }
        ...
    }