2011-01-02 6 views
9

Window1.xaml (주 윈도우)에서 AddUsers.xaml (사용자 정의 컨트롤)과 Viewusers.xaml (사용자 정의 컨트롤)을 프로그래밍 방식으로 전환하고 싶습니다.WPF에서 프로그래밍 방식으로 Windows xaml에 사용자 정의 컨트롤/페이지 추가

iam이 Window1.xaml의 버튼 이벤트로 사용자 컨트롤을 전환하려고했습니다. 나는 그물에 아무것도 발견하지 못했고, 그래서 어떤 몸이라도 나를 도울 수있다.

내 Window1. XAML 내가 두 사용자가 addUser.xaml 및 viewUser.xaml

ADDUSER을 제어이이

<Window x:Class="SwitchUsers.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="500" Width="525"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="100" /> 
     <RowDefinition Height="400*" /> 
    </Grid.RowDefinitions> 
    <StackPanel Grid.Row="0" > 
    <Button Content="Swithc User Control " Height="23" HorizontalAlignment="Right"  Margin="0,40,284,0" Name="btnSwittch" VerticalAlignment="Center" Width="168" /> 
    </StackPanel> 

    <StackPanel Grid.Row="1" > 
     <!--Here I want to display two user controls by switching from button on Top -->    
    </StackPanel> 
</Grid> 

처럼 간다. XAML 코드 :

<UserControl x:Class="SwitchUsers.addUser" 
     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" 
     d:DesignHeight="300" d:DesignWidth="300" Loaded="UserControl_Loaded"> 
    <Grid> 
    <TextBlock Height="23" HorizontalAlignment="Left" Margin="29,79,0,0" Name="textBlock1" Text=" Enter Your Name" VerticalAlignment="Top" /> 
    <TextBlock Height="23" HorizontalAlignment="Left" Margin="29,105,0,0" Name="textBlock2" Text="Enter Your Password" VerticalAlignment="Top" /> 
    <TextBox Height="23" HorizontalAlignment="Left" Margin="144,76,0,0" Name="txtBxName" VerticalAlignment="Top" Width="120" /> 
    <TextBox Height="23" HorizontalAlignment="Left" Margin="144,105,0,0" Name="txtBxPassword" VerticalAlignment="Top" Width="120" /> 
    <Button Content="Log In" Height="23" HorizontalAlignment="Left" Margin="171,160,0,0" Name="btnLogin" VerticalAlignment="Top" Width="93" /> 
    </Grid> 
    </UserControl> 

내 두 번째 사용자 CONTROLE viewUser.xaml

<UserControl x:Class="SwitchUsers.viewUser" 
     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" 
     d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
    <!-- I hidden all UI Controls here to keep code short -->  
    </Grid> 
</UserControl> 

답변

11

먼저 당신이

에 그런

<StackPanel Name="myStack" Grid.Row="1" > 
</StackPanel> 

는 당신이 필요로 이름 귀하의 스택 패널 비슷한 줄 필요

public partial class MainWindow : Window 
{ 
    private addUser _addUser; 
    private viewUser _viewUser; 
    private Control _currentUser; 

    public MainWindow() 
    { 
     InitializeComponent(); 

     _addUser = new addUser(); 
     _viewUser = new viewUser(); 
     _currentUser = _viewUser; 
     myStack.Children.Add(_currentUser); 
    } 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     myStack.Children.Clear(); 
     if (_currentUser == _addUser) 
     { 
      _currentUser = _viewUser; 
     } 
     else 
     { 
      _currentUser = _addUser; 
     } 
     myStack.Children.Add(_currentUser); 
    } 

} 
+0

@ John Petrak, 오류가 발생했습니다 – Panindra

+0

@Joun Petrak, 위의 코드와 오류가 발생합니다. 오류 캡션 "ArgumentNullExecption이 처리되지 않았습니다."및 메시지가 "값을 null 일 수 없습니다. 매개 변수 이름 : 'System.Windows.Controls.UIElementCollection'의 자식은 null 일 수 없습니다. 객체가 예상 한 UIElement에서 파생되었습니다. " – Panindra

+0

Iam 죄송합니다. 코딩하는 동안 내 실수였습니다. 이제 usercontrol에 databse의 데이터를로드하는 UI 컨트롤이있는 경우 퍼포 로스에 영향을 미칠 수 있습니다 ... applcation이 중단됩니까? if 그래서이 스위치를 수행하기 위해 백그라운드 작업자를 사용할 수 있습니다. usercontrol – Panindra

관련 문제