【问题标题】:How to close dialog window from inside UserControl如何从 UserControl 内部关闭对话框窗口
【发布时间】:2013-07-24 12:22:28
【问题描述】:

我有一个带有取消和确定按钮的用户控件。就像例如wpf扩展工具包中的Wizard控件。

当用户在 UserControl 中单击取消或确定时,如何使用此控件关闭窗口?

我无法在 OnOk 处理程序中设置 DialogResult = true,因为我的控件不是窗口。我是否必须沿着可视化树才能找到窗口,还是有更好的解决方案?

控件:

<UserControl x:Class="WizardTest.WizardControl"
         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">
<DockPanel>
    <UniformGrid DockPanel.Dock="Bottom" Columns="2">
        <Button Click="OnCancel">Cancel</Button>
        <Button Click="OnOk">Finish</Button>
    </UniformGrid>
</DockPanel>

【问题讨论】:

    标签: .net xaml user-controls


    【解决方案1】:

    你需要找到活跃的Window

    var w = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
    if (w == null) { return; }
    
    w.Close();
    

    【讨论】:

      猜你喜欢
      • 2018-09-23
      • 2023-04-01
      • 1970-01-01
      • 2017-06-07
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多