我怎样才能结合我的控制,即关闭窗口X按钮一个按钮? 我只是想创建取消按钮,只是关闭窗口。 我用我的代码MVVM。 如果可能的话只有在XAML中做到这一点,我只是不与按钮点击任何特殊代码。
Answer 1:
你可以只调用Close()
方法,这将关闭该窗口。
private void MyButton_Click(object s, RoutedEventArgs e)
{
Close();
}
Answer 2:
如果它是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"
-
<Button Content="Cancel">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction
TargetObject="{Binding ElementName=window}"
MethodName="Close"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
希望这可以帮助。
Answer 3:
没有代码隐藏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);
}
}
但是SystemCommands是.NET的4.5 ,所以如果你在一些旧版本的摇滚.NET ,你也可以使用以下。
public ICommand CloseWindowCommand
{
get
{
return new RelayCommand<Window>((window) => window.Close());
}
}
Answer 4:
由金块安装Microsoft.Expression.Interactions和克里斯·W.以上使用应答红粉。
文章来源: How to Bind to window's close button the X-button