2

我有一个带有取消和确定按钮的 UserControl。例如 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>

4

1 回答 1

1

您需要进入活动状态Window

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

w.Close();
于 2013-07-24T12:26:36.040 回答