.net 如何从UserControl内部关闭对话框窗口

.net 如何从UserControl内部关闭对话框窗口,.net,xaml,user-controls,.net,Xaml,User Controls,我有一个UserControl,它有一个cancel和ok按钮。例如,wpf扩展工具包中的向导控件 当用户在UserControl中单击cancel或ok时,如何使用此控件关闭窗口 我无法在OnOk处理程序中设置DialogResult=true,因为我的控件不是窗口。为了找到窗口,我必须沿着视觉树走,还是有更好的解决方案 控件: <UserControl x:Class="WizardTest.WizardControl" xmlns="http://schemas.m

我有一个UserControl,它有一个cancel和ok按钮。例如,wpf扩展工具包中的向导控件

当用户在UserControl中单击cancel或ok时,如何使用此控件关闭窗口

我无法在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>

取消
完成

您需要进入活动的
窗口

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

w.Close();
var w=Application.Current.Windows.OfType().SingleOrDefault(x=>x.IsActive);
如果(w==null){return;}
w、 Close();

您需要进入活动的
窗口

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

w.Close();
var w=Application.Current.Windows.OfType().SingleOrDefault(x=>x.IsActive);
如果(w==null){return;}
w、 Close();