gpt4 book ai didi

c# - 访问 UserControl 中的主窗口控件

转载 作者:太空狗 更新时间:2023-10-29 23:39:31 24 4
gpt4 key购买 nike

我的项目中有 Window 和三个 UserControl,我有一个控件本身显示 usercontrol

<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>

UserControl 中我有 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>

如何从 UserControl 控制 pageTransitionControlpageTransitionControl 导航到其他 userControl

编辑:

主窗口代码隐藏:

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

只是我想通过单击 UserControl 中的按钮在 mainWindow 中运行 pageTransitionControl 的方法 ShowPage

最佳答案

您可以从后面的 UserControls 代码中找到这样的 PageTransition 控件:

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);
}
}

然后你可以像这样使用它:

this.FindPageControl(this).ShowPage(...);

关于c# - 访问 UserControl 中的主窗口控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17520240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com