如何确定哪个View执行了命令
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我的理解是否正确。但是,您可以实现
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.是否只需将
Close()
方法公开,以便其他对象可以指定关闭行为应该是什么?InformationViewModel
中的类似内容:然后您可以在其他视图模型中使用它,如下所示:
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
:Then you can use it from within your other view models like this: