在Silverlight 4中将静态项绑定到MVVM中的组合框 [英] Binding Static Items to combobox in MVVM in Silverlight 4
本文介绍了在Silverlight 4中将静态项绑定到MVVM中的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Silverlight应用程序中绑定MVVM体系结构中的静态项。
How to bind static items in MVVM architecture in silverlight application.
推荐答案
创建一个类,其中包含您提到的两个属性,如
Create a class which holds the two properties as you mentioned like
class ComboData
{
string Text;
string Value;
}
In your ViewModel,
create a collection like
Public ObservableCollection<combodata> ComboColl = new ObservableCollection<combodata>;
</combodata></combodata>
向此集合添加数据
Add data to this collection
ComboColl.Add(new ComboData(){ Text="US",Value="US"});
在你的xaml页面中,
define用于保存两个文本值的模板,例如
In youe xaml page,
define a template to hold the two text values like
<datatemplate x:key="ComboTemplate" xmlns:x="#unknown">
<stackpanel orientation="Horizontal">
<textblock text="{Binding Text}" />
<textblock text="{Binding Value}" />
</stackpanel>
</datatemplate>
将此模板分配给组合框,例如ItemTemplate ={StaticResource ComboTemplate}
并将组合框的datacontext设置为Viewmodel对象。
希望这有助于......
Assign this template to combobox like ItemTemplate="{StaticResource ComboTemplate}"
and set the datacontext of the combobox to the Viewmodel object.
Hope this helps...
这篇关于在Silverlight 4中将静态项绑定到MVVM中的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文