[英]How to refer to another model in xaml?
I am using MVVM and embedding models within XAML, some models are constructed standalone, but one of the models need to refer to another one in construction, because the data in db also needs to refer to it, I couldn't find a way to do it in XAML, the XAML is like this: 我在XAML中使用MVVM和嵌入模型,有些模型是独立构建的,但其中一个模型需要在构造中引用另一个模型,因为db中的数据也需要引用它,我找不到方法在XAML中做,XAML是这样的:
<models:UserModel x:Key="UserModel"></models:UserModel>
<models:OrderModel x:Key="OrderModel">
<x:Arguments>
{StaticResource UserModel} // what tag to use here?
</x:Arguments>
</models:OrderModel>
Is there any right way to do this? 有没有正确的方法来做到这一点?
Because StaticResource
is a markup extension, you can use it either through attribute usage, or element usage 因为StaticResource
是标记扩展,所以您可以通过属性使用或元素使用来使用它
<models:OrderModel x:Key="OrderModel">
<x:Arguments>
<StaticResource Key="UserModel" />
</x:Arguments>
</models:OrderModel>
EDIT - 1 编辑 - 1
Looks like when you use StaticResource
inside x:Arguments
- it just passes on the extension object to the constructor; 看起来当你在x:Arguments
使用StaticResource
时 - 它只是将扩展对象传递给构造函数; instead of resolving it for value. 而不是为了价值而解决它。 Simplest way to resolve this would be to add a property to OrderModel
to assign the UserModel
object. 解决此问题的最简单方法是向OrderModel
添加属性以分配UserModel
对象。
<local:UserModel x:Key="UserModel" />
<local:OrderModel x:Key="OrderModel" User="{StaticResource UserModel}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.