Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to main window control in UserControl

I have Window and three UserControl in my Project, I have a control that show usercontrol in itself

<Window x:Class="Hesabdar.winMain"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:pageTransitions="clr-namespace:WpfPageTransitions;assembly=WpfPageTransitions"
        Title="winMain" Height="500" Width="600" Loaded="Window_Loaded_1">
    <Grid>
        <pageTransitions:PageTransition Name="pageTransitionControl"  TransitionType="SlideAndFade" />
    </Grid>
</Window>

and in UserControl I have Button:

<UserControl x:Class="Hesabdar.ucMain"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Height="500" Width="600">
    <Grid>
          <Button Content="Manege" HorizontalAlignment="Left" Margin="391,163,0,0" Click="Button_Click_1"/>
    </Grid>
</UserControl>

How is it possible control pageTransitionControl from UserControl to navigate pageTransitionControl to other userControl

Edit:

Code Behind of MainWindow :

ucMain objUC = new ucMain(); //Declare Instance Of user Control 
pageTransitionControl.ShowPage(objUC); // Show Instance of usercontrol in PageTransitionControl

Just I want to run method ShowPage of pageTransitionControl in mainWindow from click of button that is in UserControl.

like image 937
Moslem7026 Avatar asked Nov 02 '22 18:11

Moslem7026


1 Answers

You could find the PageTransition control like this from the UserControls code behind:

public static PageTransition FindPageControl(DependencyObject child)
{
    DependencyObject parent= VisualTreeHelper.GetParent(child);

    if (parent == null) return null;

    PageTransition page = parent as PageTransition;
    if (page != null)
    {
        return page;
    }
    else
    {
        return FindPageControl(parent);
    }
}

Then you can use it like this:

this.FindPageControl(this).ShowPage(...);
like image 100
Romano Zumbé Avatar answered Nov 11 '22 06:11

Romano Zumbé