简体   繁体   English

如何在xaml中引用另一个模型?

[英]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.

相关问题 如何在XAML中引用另一个依赖项属性? - How to refer to another dependency property in XAML? 如何从另一个类引用main xaml - how to refer to main xaml from another class 如何在 Silverlight XAML 中引用另一个名称空间中的绑定转换器? - How can I refer to a binding converter in another namespace in Silverlight XAML? 如何在WPF的“用户控制”页中同时引用视图模型和全局syles.xaml页 - how to refer both view model and global syles.xaml page in the User Control page in WPF UWP:- 如何引用 XAML 中另一个程序集中存在的资源字典背后的代码? - UWP:- How to refer a code behind of a resource dictionary that exists in another assembly in a XAML? 如何从XAML引用嵌入式资源? - How to refer to Embedded Resources from XAML? 如何在另一个XAML文件中加载XAML文件 - How to load a XAML file in another XAML file 如何从另一个xaml样式表引用xaml样式表 - How to reference xaml stylesheet from another xaml stylesheet 如何从一个xaml文件访问RichTextBox到另一个xaml文件? - how to access RichTextBox from one xaml file to another xaml file? 如何在Prism WPF中将类库ResourceDictionary xaml样式引用到Shell窗口 - How to refer class library ResourceDictionary xaml style to Shell window in Prism WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM