gpt4 book ai didi

c# - 如何从用户控件 View 模型中关闭窗口?

转载 作者:太空狗 更新时间:2023-10-30 00:08:14 25 4
gpt4 key购买 nike

我有一个窗口,其中包含各种 UserControl 作为页面。是否可以从用户控件的数据上下文中关闭我没有引用的窗口?简化的细节:

设置窗口

    public SetupWindow()
{
InitializeComponent();
Switcher.SetupWindow = this;
Switcher.Switch(new SetupStart());
}

public void Navigate(UserControl nextPage)
{
this.Content = nextPage;
}

设置启动用户控件

<UserControl x:Class="...">
<UserControl.DataContext>
<local:SetupStartViewModel/>
</UserControl.DataContext>
<Grid>
<Button Content="Continue" Command="{Binding ContinueCommand}"/>
</Grid>
</UserControl>

设置启动 View 模型

    public SetupStartViewModel()
{
}

private bool canContinueCommandExecute() { return true; }

private void continueCommandExectue()
{
Switcher.Switch(new SetupFinish());
}

public ICommand ContinueCommand
{
get { return new RelayCommand(continueCommandExectue, canContinueCommandExecute); }
}

最佳答案

我设法从这里的答案中找到解决方案:How to bind Close command to a button

View 模型:

public ICommand CloseCommand
{
get { return new RelayCommand<object>((o) => ((Window)o).Close(), (o) => true); }
}

查看:

<Button Command="{Binding CloseCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" Content="Close"/>

关于c# - 如何从用户控件 View 模型中关闭窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11048061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com