Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何绑定到window';s关闭按钮X按钮_C#_Wpf_Xaml_Mvvm_.net 4.5_.net - Fatal编程技术网

C# 如何绑定到window';s关闭按钮X按钮

C# 如何绑定到window';s关闭按钮X按钮,c#,wpf,xaml,mvvm,.net-4.5,.net,C#,Wpf,Xaml,Mvvm,.net 4.5,.net,如何将控件上的一个按钮绑定到关闭窗口的X按钮? 我只想创建一个只关闭窗口的取消按钮。 我在代码中使用MVVM。 如果可能只在xaml中执行,我只是没有任何特殊的代码来单击按钮。您可以调用Close()方法,该方法将关闭窗口 private void MyButton_Click(object s, RoutedEventArgs e) { Close(); } 如果是WPF(如果我记得正确的话),您可以将父级的CallMethodAction用作行为,并通过XAML使用Close()方

如何将控件上的一个按钮绑定到关闭窗口的X按钮? 我只想创建一个只关闭窗口的取消按钮。 我在代码中使用MVVM。
如果可能只在xaml中执行,我只是没有任何特殊的代码来单击按钮。

您可以调用
Close()
方法,该方法将关闭窗口

private void MyButton_Click(object s, RoutedEventArgs e)
{
    Close();
}
如果是WPF(如果我记得正确的话),您可以将父级的
CallMethodAction
用作行为,并通过XAML使用Close()方法。有点像

父窗口
x:Name=“Window”

名称空间

 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
 xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
-



希望这能有所帮助。

没有代码隐藏的MVVM解决方案也可能是这样的:

查看:

<Button Content="Cancel" Command="{Binding CloseWindowCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
public ICommand CloseWindowCommand
{
    get
    {
        return new RelayCommand<Window>(SystemCommands.CloseWindow);
    }
}

视图模型:

<Button Content="Cancel" Command="{Binding CloseWindowCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
public ICommand CloseWindowCommand
{
    get
    {
        return new RelayCommand<Window>(SystemCommands.CloseWindow);
    }
}
public ICommand CloseWindowCommand
{
得到
{
返回新的RelayCommand(SystemCommands.CloseWindow);
}
}
但SystemCommands来自于,所以,如果您使用的是旧版本的,您也可以使用以下命令

public ICommand CloseWindowCommand
{
    get
    {
        return new RelayCommand<Window>((window) => window.Close());
    }
}
public ICommand CloseWindowCommand
{
得到
{
返回新的RelayCommand((window)=>window.Close());
}
}

别忘了,你需要在你的项目中引用适当的混合库才能使用这个井该死的,我只按照xaml的操作规范去回答它,甚至无法获得投票权哈哈,这是白费力气。+1但我不会命名我的窗口。。。我会使用这个绑定
TargetObject=“{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}}}}”
还有一点不错,我只是在抱怨,但感谢fella的建议:不要害怕将代码放在MVVM应用程序的代码隐藏中。如果您正在实现的逻辑是视图专有的(在本例中,是的,因为您正在关闭窗口),那么它属于codebehind,而不是viewmodel。这在技术上打破了MVVM模式,因为viewmodel不应该有任何对视图程序集的引用(例如System.Windows.*)@SpikeX我不想争辩,我知道在这种情况下,这不是最好的解决方案,甚至不需要在VM中执行,但我不会说MVVM是如此严格。在虚拟机中不使用System.Windows.Threading?或者应用程序。调度器?不。绑定是线程安全的,可以发生在非UI线程的线程上,因此不需要dispatcher。如果您需要使用dispatcher,您将向视图发送一条解耦消息,视图将处理它。@SpikeX不是那么快xD ICommand来自System.Windows.Input命名空间。。。你不在虚拟机中使用它吗?是的,我知道这是不同的装配。这不是我的观点。在我看来,MVVM不仅仅是关于应该在哪里使用什么。。。这是关于它给你带来了什么。例如,我记得两年前,他们嘲笑Prism guy使用代码隐藏。我也从未使用过代码隐藏。从来都不需要它。我宁愿使用行为使其可重用。但还是。。。上面的代码让我失去了MVVM的什么优势?哪里写的我不能在VM中使用Window类?我希望它不是在石头的某个地方。我可以完成我的虚拟机,不需要关心视图的外观。另外,在关闭窗口之前,我还可以轻松地做其他一些事情。我想不出为什么我不能在VM中使用Window对象。