LambrentU
LambrentU

Reputation: 1

How to control the window behavior when using user control

I have a user control that conatin a button control, when clicking on that button I want to hide the window that presented right now and show another.. Since the button is in the user control "this.hide" will have no meaning because the user control is not the window itself:

<UserControl x:Name="firstUC"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Pres"
    xmlns:syncfusion ="clr-namespace:Syncfusion.UI.Xaml.Charts;assembly=Syncfusion.SfChart.WPF"
    mc:Ignorable="d" x:Class="Pres.CRUserControl" Height="643.287" Width="1250.5">


<StackPanel Margin="0,0,2,2" RenderTransformOrigin="0.511,0.536">
    <StackPanel.Background>
        <ImageBrush/>
    </StackPanel.Background>
    <Grid Height="612" x:Name="NewGrid">
        <Grid.Background>
            <ImageBrush ImageSource="Pictures/winBack.jpg" Stretch="UniformToFill"/>
        </Grid.Background>
        <Button Style="{StaticResource ButtonStyle}" HorizontalAlignment="Left" Height="92" Margin="346,221,0,0" VerticalAlignment="Top" Width="290" Opacity="1" Click="Button_Click" >

            <Button.Background>
                <ImageBrush ImageSource="Pictures/try.PNG"/>
            </Button.Background>


        </Button>
     </Grid>
  </stackpanel>
</usercontrol>

The content of the button event should be:

private void Button_Click(object sender, RoutedEventArgs e)
  {
   Area.MainWindow myWindow = new Area.MainWindow();
    myWindow.Show();
    this.Hide();
  }

Upvotes: 0

Views: 39

Answers (1)

Andy
Andy

Reputation: 12276

You can try

private void Button_Click(object sender, RoutedEventArgs e)
  {
    Area.MainWindow myWindow = new Area.MainWindow();
    myWindow.Show();
    Window.GetWindow(this).Hide();
  }

Upvotes: 1

Related Questions