Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In My WPF Aplication,

Here is one example,

I am having One mainwindow and two UserControls named One And Two.

In my Main window XAML code like,

XML
<Window x:Class="WpfApplication9.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfApplication9">
    <Grid>
        <my:One HorizontalAlignment="Left" Margin="266,88,0,0" x:Name="one1" VerticalAlignment="Top" />
        <my:Two HorizontalAlignment="Left" Margin="254,216,0,0" x:Name="two1" VerticalAlignment="Top" Visibility="Hidden"/>
    </Grid>
</Window>


At the same time Usercontrol--One.xaml code contains
XML
<UserControl x:Class="WpfApplication9.One"
             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="140" d:DesignWidth="188">
    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="53,53,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" FontFamily="TeX Gyre Adventor" FontWeight="Bold" />
    </Grid>
</UserControl>


My requirement is when the user clicks on the button1 we need to show the usercontrol two on the main window.

kindly tell me is this possible, if possible which way should i proceed.
Posted

1 solution

Hi it's very easy.it's not suitable raiseEvent in userControls.
you can do this.your mainWindow.xaml must be like following:
XML
<my:one x:name="OneUserControl" buttonbase.click="Show" />
<my:two x:name="TwoUserControl" visibility="Hidden" />



your mainWindow.cs must be like :

private void Show(object sender,RoutedEventArgs e)
{
Button button=e.OriginalSource as Button;
if(button==null)
{
return ;
}
else
{
TwoUserControl.Visibility=Visibility.Visible;
}
}
GoodLuck
 
Share this answer
 
v4
Comments
D-Kishore 6-Mar-13 0:30am    
Yes its fine,

but In my application TwoUserControl is dependent on the OneUserControl.
When I use above code at the same time two controls are loading.

Once the user clicks on the button from OneUserControl then only TwoUserControl should load.

can you have any idea about this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900