C# 在WPF中创建自定义关闭按钮

C# 在WPF中创建自定义关闭按钮,c#,wpf,button,user-interface,C#,Wpf,Button,User Interface,我需要实现一个自定义窗口装饰器,其中一个关闭按钮将与所有Windows应用程序上的关闭按钮或x按钮完全相同。只需从按钮调用Close()函数: WPF: 如果在当前视图中添加按钮,则可以从代码隐藏中执行以下操作: var closeButton = new Button(); closeButton.Click += closeButton_Click; // Add the button to the window Content = closeButton; 然后您可以响应该事件,只需按

我需要实现一个自定义窗口装饰器,其中一个关闭按钮将与所有Windows应用程序上的关闭按钮或x按钮完全相同。

只需从按钮调用
Close()
函数:

WPF:


如果在当前视图中添加按钮,则可以从代码隐藏中执行以下操作:

var closeButton = new Button();
closeButton.Click += closeButton_Click;

// Add the button to the window
Content = closeButton;
然后您可以响应该事件,只需按如下方式调用
Close()

void closeButton_Click(object sender, RoutedEventArgs e)
{
    Close();
}
Button Command="{Binding MainCloseButtonCommand}"
CommandParameter="{Binding ElementName=mainWindow}"

private void performMainCloseButtonCommand(object Parameter)
{
    Window objWindow  = Parameter as Window;
    objWindow.Close();
}
<Window x:Class="MyProjectNamespace.MainWindow" 
        xmlns:local="clr-namespace:MyProjectNamespace">

    <Button Name="CloseButton"
            local:ButtonBehavior.IsClose="True" ... />
它的基本功能是在
窗口中添加一个按钮
/
用户控件
,当您按下它时,它将关闭窗口

如果从
XAML
执行此操作,它可能如下所示:

void closeButton_Click(object sender, RoutedEventArgs e)
{
    Close();
}
Button Command="{Binding MainCloseButtonCommand}"
CommandParameter="{Binding ElementName=mainWindow}"

private void performMainCloseButtonCommand(object Parameter)
{
    Window objWindow  = Parameter as Window;
    objWindow.Close();
}
<Window x:Class="MyProjectNamespace.MainWindow" 
        xmlns:local="clr-namespace:MyProjectNamespace">

    <Button Name="CloseButton"
            local:ButtonBehavior.IsClose="True" ... />
实际上是这样的:

<Window Name = "Chrome">
   .
   .
  <Button Name="closeButton" onClick="terminateApplication"/>
   .
</Window>

如果希望使用MVVM体系结构,则可以将窗口的名称作为命令参数传递,并在命令中关闭窗口

代码如下所示:

void closeButton_Click(object sender, RoutedEventArgs e)
{
    Close();
}
Button Command="{Binding MainCloseButtonCommand}"
CommandParameter="{Binding ElementName=mainWindow}"

private void performMainCloseButtonCommand(object Parameter)
{
    Window objWindow  = Parameter as Window;
    objWindow.Close();
}
<Window x:Class="MyProjectNamespace.MainWindow" 
        xmlns:local="clr-namespace:MyProjectNamespace">

    <Button Name="CloseButton"
            local:ButtonBehavior.IsClose="True" ... />

如果需要关闭对话框
窗口的按钮
,可以为他添加
IsCancel
属性:

<Button Name="CloseButton"
        IsCancel="True" ... />
窗口中
如下使用:

void closeButton_Click(object sender, RoutedEventArgs e)
{
    Close();
}
Button Command="{Binding MainCloseButtonCommand}"
CommandParameter="{Binding ElementName=mainWindow}"

private void performMainCloseButtonCommand(object Parameter)
{
    Window objWindow  = Parameter as Window;
    objWindow.Close();
}
<Window x:Class="MyProjectNamespace.MainWindow" 
        xmlns:local="clr-namespace:MyProjectNamespace">

    <Button Name="CloseButton"
            local:ButtonBehavior.IsClose="True" ... />


现在可以通过单击按钮或按Esc键关闭
主窗口
,这一切都独立于
视图
(UI)。

将关闭按钮与viewmodel一起使用。视图模型不依赖于gui

namespace YourLibrary.ComponentModel
{
   /// <summary>
  /// Defines Close method. Use for window, file stream,.... 
  /// </summary>
  public interface IClosable
  {
      /// <summary>
      /// Close of instance (window, file stream,... etc).
      /// </summary>
      void Close();
  }
MyViewModel.cs

public class MyViewModel 
{
  ICommand buttonCommand;
  /// <summary>
  /// Command for Button.
  /// Default action is close the window.  
  /// Close window via retyping parameter as <see cref="IClosable"/> 
  /// </summary>
  public ICommand ButtonCommand => buttonCommand ?? (buttonCommand=new RelayCommand(ButtonCommandExecute));
  void ButtonCommandExecute (object obj) 
  {
      var myWindow = (IClosable)obj;
      myWindow.Close();
  };

我对此有什么不明白的?如果您在窗口的代码隐藏区内,则不需要按名称调用它。因此,您的代码基本上是其他答案中代码的较长版本。恐怕你的答案有被否决的危险。这是一个更“一般化”的答案。。。。还有,当我写这篇文章的时候,又有两个答案涌了进来我尝试了command=“close”,但什么也没发生。要让command=“close”实际关闭窗口,是否还需要执行其他操作?