2013-07-08 15 views
5

Tôi có Window và ba UserControl trong dự án của tôi, tôi có một điều khiển hiển thị usercontrol của riêng mìnhTiếp cận kiểm soát cửa sổ chính trong 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> 

và trong UserControl Tôi có 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> 

Làm cách nào để có thể kiểm soát pageTransitionControl từ UserControl để điều hướng pageTransitionControl sang khác userControl

Edit:

Mã Đằng sau của MainWindow:

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

Chỉ cần tôi muốn chạy phương pháp ShowPage của pageTransitionControl trong MainWindow từ nhấp chuột vào nút đó là trong UserControl của.

+0

Hãy xem trong câu trả lời tôi đã đưa ra cho câu hỏi của tôi trong chủ đề này: http://stackoverflow.com/questions/17059120/change-wpf-usercontrol-depending-on-a-property-of-a-treeviewitem –

+0

có vẻ như rằng mục tiêu của tôi khác với bạn, Thêm thông tin Đã thêm @Romano Zumbé – Moslem7026

Trả lời

2

Bạn có thể tìm thấy sự kiểm soát PageTransition như thế này từ UserControls mã sau:

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

Sau đó, bạn có thể sử dụng nó như thế này:

this.FindPageControl(this).ShowPage(...); 
+0

nó hoạt động! bạn cast parent như 'PageTransition', nhưng như tôi biết cha mẹ của điều khiển này là window.as tôi biết nó là đệ quy mã nhưng làm thế nào diễn viên đã được thực hiện và không có lỗi xảy ra!? – Moslem7026

+1

Nó hoạt động vì toán tử 'as', điều này không gây ra ngoại lệ nếu diễn viên là không thể. Thay vào đó nó trả về một giá trị 'null'. –

0

Điều bạn thực sự cần là sử dụng mẫu MVVM. Thay vào đó hãy sử dụng một ContentControl. Và chuyển tham chiếu usercontrol tới thuộc tính Nội dung của nó. bạn có thể lấy ý tưởng từ nó, như:

yourContentControl.Content = new UserControl1(); 
+0

Mã đã chỉnh sửa và thông tin thêm được thêm – Moslem7026