Mos*_*026 5 c# wpf user-controls
我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>
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
它是如何可能的控制pageTransitionControl从UserControl导航pageTransitionControl到其他userControl
编辑:
MainWindow背后的代码:
ucMain objUC = new ucMain(); //Declare Instance Of user Control
pageTransitionControl.ShowPage(objUC); // Show Instance of usercontrol in PageTransitionControl
Run Code Online (Sandbox Code Playgroud)
只是我想运行方法ShowPage的pageTransitionControl从按钮是在单击主窗口中UserControl.
您可以从 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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样使用它:
this.FindPageControl(this).ShowPage(...);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2969 次 |
| 最近记录: |