如何通过xaml文件中的“取消”按钮关闭窗口?

时间:2016-10-17 13:15:09

标签: c# wpf xaml user-interface button

我想在xaml文件中关闭窗口/对话框。实际xaml: ...

<Button Name="CancelButton" Content="Cancel" Controls1:ButtonUtils.DialogResult="False" />

没有实现此按钮的功能,例如:

<Button Name="CancelButton" Content="Cancel" Controls1:ButtonUtils.DialogResult="False" Click="CancelButton_Click" />

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
    Close();
}

现在CANCEL按钮与OK按钮完全相同(保存内容并关闭窗口)。我想改变一下的行为 取消按钮 - 只是关闭窗口。除了xaml文件之外,CANCEL按钮没有实现。 带OK和CANCEL按钮的XAML: ...

<Controls:WindowFooter>
    <Button Content="Ok" Name="OkButton" IsDefault="true" Controls1:ButtonUtils.DialogResult="True" >
        <Button.IsEnabled>
            <MultiBinding>
                <MultiBinding.Converter>
                    <Converters:ErrorMultiConverter/>
                </MultiBinding.Converter>
                <Binding ElementName="test1" Path="(Validation.HasError)"/>
                <Binding ElementName="test2" Path="(Validation.HasError)"/>
                <Binding ElementName="test3" Path="(Validation.HasError)"/>
                <Binding ElementName="test4" Path="(Validation.HasError)"/>
            </MultiBinding>
        </Button.IsEnabled>
    </Button>
    <Button Name="CancelButton" Content="Cancel" Controls1:ButtonUtils.DialogResult="False" />
</Controls:WindowFooter>

我期待的是:

<Button Name="CancelButton" Content="Cancel" Controls1:ButtonUtils.DialogResult="False"       [Method=CloseWindow()]      />

1 个答案:

答案 0 :(得分:2)

也许Button.IsCancel属性可以帮到你吗?

<Button Name="CancelButton" Content="Cancel" Controls1:ButtonUtils.DialogResult="False" IsCancel=True"/>