如何确定哪个View执行了命令

发布于 2024-11-29 04:11:30 字数 805 浏览 0 评论 0原文

我有一个名为 InformationView.xaml 的视图,并且该视图被重新用于显示来自 3 个不同源的相同信息(每个视图都有不同的窗口标题)。每个视图的数据上下文都设置为一种 ViewModel 类型的相同实例。在我的 ViewModel 类中,我有一个 ICommand 属性,视图内的“关闭”按钮绑定到该属性。 -- 有没有办法确定命令的发送者是谁? (具体来说,按窗口标题)。

这是一个示例:

我有一个带有以下按钮的视图类(“注意:每个视图将有不同的窗口标题/显示来自不同源的数据 - 但使用相同的视图)

<Button Width="75" Height="23" Margin="0,0,5,5" Content="Close" Command="{Binding CloseCommand}" />

我有一个带有以下按钮的 ViewModel 类命令

    public ICommand CloseCommand
    {
        get
        {
            if (this._closeCommand == null)
            {
                this._closeCommand = new RelayCommand(Command => this.OnClose());
            }     
            return _closeCommand;
        }
    }

我正在寻找一种方法来确定哪个窗口执行该命令(我将拥有使用相同 ViewModel 的多个视图实例)。

I have a View called InformationView.xaml and this same View is re-used to display the same information from 3 different sources (each view has a different window title). Each View has their datacontext set the same instance of one ViewModel type. Within my ViewModel class, I have an ICommand property that the 'Close' button inside the View is bound to. -- Is there a way to determine who the sender was of the command? (specifically, by window title).

Here is an example:

I have a view class with the following button ("Note: each View will have a different window title / display data from a different source--but the same View is used)

<Button Width="75" Height="23" Margin="0,0,5,5" Content="Close" Command="{Binding CloseCommand}" />

I have a ViewModel class with the following command

    public ICommand CloseCommand
    {
        get
        {
            if (this._closeCommand == null)
            {
                this._closeCommand = new RelayCommand(Command => this.OnClose());
            }     
            return _closeCommand;
        }
    }

I am looking for a way to determine which window executed the command (I will have multiple instances of the View using the same ViewModel).

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

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

发布评论

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

评论(2

错々过的事 2024-12-06 04:11:30

我不确定我的理解是否正确。但是,您可以实现 Unloaded 事件。在该事件方法内以及命中断点时设置断点。您可以检查该视图的窗口标题属性。

I'm not sure if I understand you correctly. However, you could possibly implement the Unloaded event. Set a breakpoint inside that event method and when you hit the breakpoint. You could check the window title property for that view.

时光与爱终年不遇 2024-12-06 04:11:30

是否只需将 Close() 方法公开,以便其他对象可以指定关闭行为应该是什么?

InformationViewModel 中的类似内容:

public event EventHandler RequestClose;

void OnRequestClose()
{
    EventHandler handler = this.RequestClose;
    if (handler != null)
        handler(this, EventArgs.Empty);
}

然后您可以在其他视图模型中使用它,如下所示:

InformationViewModel.Close += CloseMethod;

public CloseMethod(object sender, EventArgs e)
{
    // Implement close logic here
}

What about just making the Close() method public so that other objects can specify what the close behavior should be?

Something along the lines of this in your InformationViewModel:

public event EventHandler RequestClose;

void OnRequestClose()
{
    EventHandler handler = this.RequestClose;
    if (handler != null)
        handler(this, EventArgs.Empty);
}

Then you can use it from within your other view models like this:

InformationViewModel.Close += CloseMethod;

public CloseMethod(object sender, EventArgs e)
{
    // Implement close logic here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文