Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding User control / Page to windows xaml programmatically in WPF

I want to switch between my two user controls AddUsers.xaml (user control) and Viewusers.xaml(user Control) programmatically in Window1.xaml (main Window).

I'm trying to switch user controls by Button Event in Window1.xaml.

my Window1. xaml goes like this

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

I have two User Controls addUser.xaml and viewUser.xaml

addUser. xaml code:

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

and my second user control 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>
like image 207
Panindra Avatar asked Jan 02 '11 07:01

Panindra


People also ask

How do I add user control to XAML?

Using a user control in XAML is pretty simple. I use a WPF Application to test the control. Create a WPF application project and copy the control code files to your project. After that, you need to add namespace of the library in which the user control is defined.

How can I access a control in WPF from another class or window?

If you want to access a control on a wpf form from another assembly you have to use the modifier attribute x:FieldModifier="public" or use the method proposed by Jean. Save this answer.

What is difference between user control and window in WPF?

A window is managed by the OS and is placed on the desktop. A UserControl is managed by wpf and is placed in a Window or in another UserControl. Applcations could be created by have a single Window and displaying lots of UserControls in that Window.


1 Answers

First you need to give your stack panel a name

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

Then you need something similar to

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

}
like image 185
John Petrak Avatar answered Oct 07 '22 02:10

John Petrak