如何在使用用户控件时控制窗口行为

时间:2018-04-25 17:53:00

标签: wpf

我有一个控制按钮控件的用户控件,当点击该按钮我想隐藏现在显示的窗口并显示另一个..由于按钮在用户控件中“this.hide”将没有意思是因为用户控件不是窗口本身:

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

按钮事件的内容应为:

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

1 个答案:

答案 0 :(得分:1)

你可以尝试

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