2016-05-12 94 views
1

我有一个WPF程序,并在某些时候我打开了一扇窗:如何从用户控件里面关闭一个窗口?

public object testCommand() 
    { 
     Window window = new Window 
     { 
      Title = "My User Control Dialog", 
      Content = new OKMessageBox("Error Message"), 
      SizeToContent = SizeToContent.WidthAndHeight, 
      ResizeMode = ResizeMode.NoResize 
     }; 
     window.WindowStartupLocation = WindowStartupLocation.CenterScreen; 
     window.ShowDialog(); 

     return null; 
    } 

用户控制XAML:

<UserControl x:Class="SolidX.Base.MessageBoxes.OKMessageBox" 
      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" 
      xmlns:local="clr-namespace:SolidX.Base.MessageBoxes" 
      xmlns:viewProperties="clr-namespace:AppResources.Properties;assembly=AppResources" 
      mc:Ignorable="d" 
      d:DesignHeight="150" d:DesignWidth="400"> 
    <Grid> 
     <TextBlock x:Name="textMessage" Margin="10"/> 
     <Grid Height="Auto" VerticalAlignment="Bottom"> 
      <Grid.RowDefinitions> 
       <RowDefinition/> 
       <RowDefinition/> 
      </Grid.RowDefinitions> 
      <CheckBox Content="Don't show this again" HorizontalAlignment="Right" Margin="5,5,10,5"/> 
      <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right"> 
       <Button x:Name="btnOk" Content="{x:Static viewProperties:Resources.Common_OK}" Height="25" VerticalAlignment="Center" Width="90" Margin="5"/> 
      </StackPanel> 
     </Grid> 
    </Grid> 
</UserControl> 

用户控件的代码隐藏:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace SolidX.Base.MessageBoxes 
{ 
    /// <summary> 
    /// Interaction logic for OKMessageBox.xaml 
    /// </summary> 
    public partial class OKMessageBox : UserControl 
    { 
     public OKMessageBox(string Message) 
     { 
      InitializeComponent(); 
      textMessage.Text= Message; 
     } 
    } 
} 

而且在我的用户控件中,我试图让按钮关闭窗口(目前,设置Button.IsCancel = true;的作品,但我希望这是一个可重用的选项 - 基本上使我自己的this.Close;)。

我想这在代码隐藏我的用户,但无济于事(由其他几个StackOverflow的答案建议)(parentWindow总是空):

var parentWindow = Window.GetWindow(this); 
parentWindow.Close(); 
+0

如果parentWindow是Windows代码它应该管用。如果您使用的是MVVM,则可能需要先由Windows拥有者进行分配。 –

+0

注意:这个问题的最后一部分看起来不再是真实的:在接受的答案下面的评论指出,问题是通过使用这个解决方案(简称:Window.GetWindow(this).Close();')来解决的 – Kjartan

回答

3

在用户控件试试这个:

XAML

<UserControl .... Loaded="UserControl_Loaded"> 
    <!-- --> 
</UserControl> 

CS

public UserControl() 
{ 
    InitializeComponent(); 
} 

private void UserControl_Loaded(object sender, RoutedEventArgs e) 
{ 
    var window = Window.GetWindow(this); 
    window.Close(); 
} 
0

如果您正在使用ViewModel类对于您的Window,您可以将ViewModel类设置为您的Window实例,并且ViewModel可以存储它的所有者。

例如视图模型:

public class CloseConfirmViewModel 
{ 
    internal Window Owner { get; set; } 

    public ICommand CloseCommand { get; private set; } // ICommand to bind it to a Button 

    public CloseConfirmViewModel() 
    { 
     CloseCommand = new RelayCommand<object>(Close); 
    } 

    public void Close() // You can make it public in order to call it from codebehind 
    { 
     if (Owner == null) 
      return; 

     Owner.Close(); 
    } 
} 

为了得到它的工作,你必须在ViewModel类设置为您Window

public partial class CloseConfirmWindow : Window 
{ 
    public CloseConfirmWindow(CloseConfirmViewModel model) 
    { 
     DataContext = model; 

     InitializeComponent(); 

     model.Owner = this; 
    } 
} 

并且要创建一个新的实例这样:

var model = new CloseConfirmViewModel(); 
var closeWindow = new CloseConfirmWindow(model); 
closeWindow.ShowDialog(); // Hopefully you've added a button which has the ICommand binded 

您可以将WindowCloseCommandUserControl的按钮,或者如果您不使用命令,请在单击UC按钮时调用Close()方法。

+0

我没有为我的窗口使用ViewModel类 - 窗口是“即时”创建的(请参阅上面的代码) – derekantrican

-1

要关闭模态对话框,你应该设置DialogResult:DialogResult = false;

+0

从哪里设置它?我的用户控制?我如何从用户控件访问它? – derekantrican

+0

好吧,好的,在编辑之前,我还没有理解你的问题。是的,您无法直接从您的控件访问它。例如,您可以尝试设置一些花哨的绑定“{Binding Path = PathToProperty,RelativeSource = {RelativeSource AncestorType = {x:Type Window}}}'''''''''与相对来源并尝试设置。 – 3615