具有参数的静态资源构造函数 [英] Static resource constructor with parameters

查看:153
本文介绍了具有参数的静态资源构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在我的XAML文件中创建一个静态资源。

I have to create in my XAML file a static resource.

<Window.Resources>
    <vm:ViewModel x:Key="viewModel" />
</Window.Resources>

我需要这个静态资源来获取我的组合框的项目

I need this static resource to get the items for my combobox

ItemsSource="{Binding Source={StaticResource viewModel}, Path=GetItems, Mode=TwoWay}"

但是我怎样才能给ViewModel(构造函数)一个类后面的代码实例呢?

But how can I give the ViewModel (constructor) a instance of my code behind class?

推荐答案

如果我正确理解,你违反了MVVM模式。
不要将ComboBox中的项目提供给您的VM。您应该提供来自VM的项目,并将其绑定到Combobox,并且您在访问项目时没有问题。

If I understand this correctly, you are violating the MVVM pattern. You should never provide items from the ComboBox into your VM. You should rather provide the items from you VM and bind it to the Combobox, and the you don't have problems accessing the items.

这篇关于具有参数的静态资源构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆