如何使用 MVVM Light Toolkit 通过取消按钮关闭 ChildWindow

发布于 2024-11-01 00:02:41 字数 683 浏览 5 评论 0原文

我是 MVVM 新手,试图弄清楚如何使用 MVVM Light Toolkit 通过传统的取消按钮来关闭 ChildWindow

在我的 ChildWindow (StoreDetail.xaml) 中,我有:

<Button x:Name="CancelButton" Content="Cancel" Command="{Binding CancelCommand}" />

在我的 ViewModel (ViewModelStoreDetail.cs) 中,我有:

public ICommand CancelCommand { get; private set; }

public ViewModelStoreDetail()
{
    CancelCommand = new RelayCommand(CancelEval);
}

private void CancelEval()
{
    //Not sure if Messenger is the way to go here...
    //Messenger.Default.Send<string>("ClosePostEventChildWindow", "ClosePostEventChildWindow");
}

I'm new to MVVM and trying to figure out how to close a ChildWindow with the traditional Cancel button using MVVM Light Toolkit.

In my ChildWindow (StoreDetail.xaml), I have :

<Button x:Name="CancelButton" Content="Cancel" Command="{Binding CancelCommand}" />

In my ViewModel (ViewModelStoreDetail.cs), I have :

public ICommand CancelCommand { get; private set; }

public ViewModelStoreDetail()
{
    CancelCommand = new RelayCommand(CancelEval);
}

private void CancelEval()
{
    //Not sure if Messenger is the way to go here...
    //Messenger.Default.Send<string>("ClosePostEventChildWindow", "ClosePostEventChildWindow");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

醉南桥 2024-11-08 00:02:41
private DelegateCommand _cancelCommand;

public ICommand CancelCommand
{
    get
    {
        if (_cancelCommand == null)
            _cancelCommand = new DelegateCommand(CloseWindow);
        return _cancelCommand;
    }
}

private void CloseWindow()
{
    Application.Current.Windows[Application.Current.Windows.Count - 1].Close();
}
private DelegateCommand _cancelCommand;

public ICommand CancelCommand
{
    get
    {
        if (_cancelCommand == null)
            _cancelCommand = new DelegateCommand(CloseWindow);
        return _cancelCommand;
    }
}

private void CloseWindow()
{
    Application.Current.Windows[Application.Current.Windows.Count - 1].Close();
}
鸠书 2024-11-08 00:02:41

如果您通过调用 ShowDialog() 显示子窗口,则只需将按钮控件的 IsCancel 属性设置为“True”即可。

<Button Content="Cancel" IsCancel="True" />

它与单击窗口上的 X 按钮或按键盘上的 ESC 相同。

If you displayed your child window by calling ShowDialog(), then you can simply set the IsCancel property of your button control to "True".

<Button Content="Cancel" IsCancel="True" />

It becomes the same as clicking the X button on the window, or pressing ESC on the keyboard.

土豪我们做朋友吧 2024-11-08 00:02:41

请查看MSDN 上的这篇文章。大约一半的地方有一个关于如何做到这一点的方法。基本上它使用 WorkspaceViewModel 或您实现一个公开和事件 RequestClose 的接口,

然后您可以在 Window 的 DataContext 内部(如果您将 ViewModel 设置为它)附加到活动。

这是文章的摘录(图 7)。您可以调整它以满足您的需要。

// In App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

MainWindow window = new MainWindow();

// Create the ViewModel to which 
// the main window binds.
string path = "Data/customers.xml";
var viewModel = new MainWindowViewModel(path);

// When the ViewModel asks to be closed, 
// close the window.
viewModel.RequestClose += delegate 
{ 
    window.Close(); 
};

// Allow all controls in the window to 
// bind to the ViewModel by setting the 
// DataContext, which propagates down 
// the element tree.
window.DataContext = viewModel;

window.Show();
}

Have a look at this articleon MSDN. About half way down there is an approach on how to do this. Basically it uses either uses a WorkspaceViewModel or you implements an interface that exposes and event RequestClose

You then inside the Window's DataContext (if you are setting the ViewModel to it) you can attach to the event.

This is an excerpt from the article (Figure 7). You can adjust it to suit your needs.

// In App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

MainWindow window = new MainWindow();

// Create the ViewModel to which 
// the main window binds.
string path = "Data/customers.xml";
var viewModel = new MainWindowViewModel(path);

// When the ViewModel asks to be closed, 
// close the window.
viewModel.RequestClose += delegate 
{ 
    window.Close(); 
};

// Allow all controls in the window to 
// bind to the ViewModel by setting the 
// DataContext, which propagates down 
// the element tree.
window.DataContext = viewModel;

window.Show();
}
葮薆情 2024-11-08 00:02:41

我已经有一段时间没有使用 WPF 和 MVVMLight 了,但是我想我应该使用消息发送器来发送取消事件。

It's been a while since I've used WPF and MVVMLight but yes I think I'd use the messanger to send the cancel event.

倦话 2024-11-08 00:02:41

MVVM Light Toolkit中,您能做的最好的事情就是使用MessengerView进行交互。

只需在 View 中注册 close 方法(通常在代码隐藏文件中),然后在需要时发送关闭窗口的请求。

In MVVM Light Toolkit the best what you can do is to use Messenger to interact with the View.

Simply register close method in the View (typically in the code behind file) and then send request to close a window when you need it.

坦然微笑 2024-11-08 00:02:41

我们实施了无代码功能背后。看看是否有帮助。

编辑:这里有 Stackoverflow 讨论

We have implemented a NO-CODE BEHIND functionality. See if it helps.

EDIT: Here is there Stackoverflow discussion

九命猫 2024-11-08 00:02:41

这里有一些方法可以实现它。

  1. 向您的子窗口发送消息,并在子窗口代码隐藏中将 DialogueResult 设置为 false。
  2. 创建 DialogueResult 的属性并将其与子窗口 Dialoue CLR 属性绑定,在 CancelCommand 的 CancelEval 方法上设置它。
  3. 创建 Childwindow 对象并在 CancelEval 上将 DialogueResult 设置为 false。

Here are some ways to accomplish it.

  1. Send message to your childwindow and set DialogueResult to false on childwindow code-behind.
  2. Make property of DialogueResult and Bind it with childwindow Dialoue CLR property, set it on CancelEval method of CancelCommand.
  3. Create object of Childwindow and set DialogueResult false on CancelEval.
死开点丶别碍眼 2024-11-08 00:02:41

参加聚会有点晚了,但我想我应该补充一下我的意见。借用 user841960 的答案:

public RelayCommand CancelCommand
{
    get;
    private set;
}

然后:

SaveSettings = new RelayCommand(() => CloseWindow());

然后:

private void CloseWindow()
{
    Application.Current.Windows[Application.Current.Windows.Count - 1].Close();
}

它比使用 ICommand 更干净,而且效果也一样。

因此,总而言之,示例类如下所示:

public class ChildViewModel
{
    public RelayCommand CancelCommand
    {
        get;
        private set;
    }

    public ChildViewModel()
    {
        SaveSettings = new RelayCommand(() => CloseWindow());
    }

    private void CloseWindow()
    {
        Application.Current.Windows[Application.Current.Windows.Count - 1].Close();
    }
}

Kind of late to the party but I thought I'd add my input. Borrowing from user841960's answer:

public RelayCommand CancelCommand
{
    get;
    private set;
}

Then:

SaveSettings = new RelayCommand(() => CloseWindow());

Then:

private void CloseWindow()
{
    Application.Current.Windows[Application.Current.Windows.Count - 1].Close();
}

It's a bit cleaner than using an ICommand and works just as well.

So, to sum it all up, the example class would look like so:

public class ChildViewModel
{
    public RelayCommand CancelCommand
    {
        get;
        private set;
    }

    public ChildViewModel()
    {
        SaveSettings = new RelayCommand(() => CloseWindow());
    }

    private void CloseWindow()
    {
        Application.Current.Windows[Application.Current.Windows.Count - 1].Close();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文