Click here to Skip to main content
15,903,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to bind static items in MVVM architecture in silverlight application.
Posted
Comments
Naz_Firdouse 3-Jan-13 6:58am    
post what you did till now so that we can help you...
srinivas.dammalapati 4-Jan-13 3:59am    
How to static binding items to Combobox in MVVM in Silverlight application.

For Example i have Country Combobox dropdown so i need to bind (Text="US", Value="US"),(Text="CA", Value="CA") in XAML class.

Please give me sample example its very urgent.

srinivas.dammalapati 4-Jan-13 6:28am    
i already wrote in Viewmodel same like above but i am looking at Xaml page like below (this is classic ASP.net but i want in silverlight).
<asp:DropDownList ID="DropDownListID" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text="static item 1" Value="1" />
<asp:ListItem Text="static item 2" Value="2" />
<asp:ListItem Text="static item 3" Value="3" />

1 solution

Create a class which holds the two properties as you mentioned like
C#
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
C#
ComboColl.Add(new ComboData(){ Text="US",Value="US"});

In youe xaml page,
define a template to hold the two text values like
HTML
<datatemplate x:key="ComboTemplate" xmlns:x="#unknown">
           <stackpanel orientation="Horizontal">
         <textblock text="{Binding Text}" />
         <textblock text="{Binding Value}" />
           </stackpanel>
       </datatemplate>


Assign this template to combobox like ItemTemplate="{StaticResource ComboTemplate}"

and set the datacontext of the combobox to the Viewmodel object.

Hope this helps...
 
Share this answer
 
Comments
srinivas.dammalapati 4-Jan-13 5:54am    
i already wrote in Viewmodel same like above but i am looking at Xaml page like below (that is classic ASP.net but i want in silverlight).
<asp:DropDownList ID="DropDownListID" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text="static item 1" Value="1" />
<asp:ListItem Text="static item 2" Value="2" />
<asp:ListItem Text="static item 3" Value="3" />
Naz_Firdouse 4-Jan-13 6:28am    
for that, you need to bind the data and set the itemsource for the combobox

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900