根据使用的触发器从 ViewModel 绑定不同的属性
我正在尝试实现一件简单的事情,或者我认为它很简单,但我不知道是否可能...
想象一下以下场景...在 ViewModel 中,我有一个名为 SelectedProduct 的属性和另一个名为 NewProduct 的属性(两者都是相同类型、型号)。
在我的 MainWindow 中,我们有一个 ListView 和两个按钮,一个是“添加”按钮,另一个是“更新”按钮(这一个将更新 ListView 中的所选项目),两者都会打开相同的窗口,但会执行不同的操作。
我在按钮中使用命令,因此我认为如果单击了主窗口的“添加”按钮或使用其他命令,我可以使用一个命令。
因此,如果我们单击“添加”按钮,第二个窗口中的控件(TextBoxes 和 ComboBoxes)将绑定 NewProduct 属性,如果我们单击“更新”按钮,第二个窗口将绑定 SelectedProduct 属性。
有什么办法可以实现这一点吗?
提前致谢
I am trying to achieve a simple thing, or i think it's simple but i don't know if it's possible...
Imagine the following scenario... In the ViewModel i have one Property called SelectedProduct and other one called NewProduct (both of the same type, the model).
In my MainWindow we have a ListView and two buttons, one is the Add button and the other is the Update button (and this one will update the Selected Item in the ListView), and both will open the same window but will do different things.
I am using Commands in the buttons, so i think i can use one Command if have clicked in the Add button of the MainWindow or use the other Command.
So if we click in the Add button, the controls (TextBoxes and ComboBoxes) in the second window will bind the NewProduct property, and if we click in the Update button the second window will bind the SelectedProduct property.
Is there any way to achieve this?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不创建一个
EditedProduct
属性并为其指定适当的值?Why don't you just create a single
EditedProduct
property and give that the appropriate value?我认为适用于这种情况的两种方法是使用两个不同的命令,正如您所说,每个命令都会以不同的方式调用
Window
。第一个选项是为
Window
创建两个构造函数(一个用于添加,另一个用于更新),或者只创建一个传入一个枚举值作为其参数。public void Window2(DisplayMode mode)
其中
DisplayMode
是一个具有两个值(Add
和Update)的
)enum
或
第二个选项是将
Window
的构造函数设为私有,并在类上创建静态方法来创建Window的实例并显示它给用户。一样使用,
这可以像
Window2.UpdateItem(selectedItem)
其中每个方法都将窗口实例的 DataContext 设置为您想要编辑的对象(或者是该对象的新实例,或者是该实例)被传递给构造函数)
Two methods I can see that will work for this scenario would be to use two different commands as you stated that will each call the
Window
differently.The first option would be to create either two constructors for the
Window
(one for Add, and the other for Update), or just create one that passes in an enum value as its parameter.public void Window2(DisplayMode mode)
Where
DisplayMode
is anenum
with two values (Add
andUpdate
)or
The second option would be to make the constructor for the
Window
private, and create static methods on the class to create an instance of the Window and display it to the user.This could be used like
Window2.UpdateItem(selectedItem)
With each of these methods setting the DataContext for the window instance to the object you wanted edited (either to a new instance of the object, or to the instance that was passed in to the constructor)
看看下面的视图模型。
您可以创建一个将绑定到您的视图的新属性,并且可以根据按钮命令更改属性引用。
在下面的代码中,我创建了一个已编辑的属性(绑定到视图),并且在执行添加按钮命令时将 Newproduct 分配给它,并在更新时分配了 SelectedProduct 。
Have a look at following view model.
You can create a new property which will be binded to your view and you can change the property reference based on your button command.
In the following code i have created a edited property (Binded to view) and i have assigned the Newproduct to it on add button command execute and assigned SelectedProduct while updating..