Sanjay Patel
Sanjay Patel

Reputation: 983

Binding Items to silverlight combobox in MVVM Architecture

i am creating one property in entity class like below

public class Myclass
{
private string _Selecteditem;
public string SelectedItem
{
get{return _Selecteditem;}
set{_Seleteditem = value;
}
}

in xaml page i am binding Combobox like below

<ComboBox Name="cmbCountry" Grid.Column="14" Grid.Row="0" Width="150" SelectedItem="{Binding SelectedCountry,Mode=TwoWay}" >
                <ComboBoxItem Tag="--Select--" Content="--Select--"/>
                <ComboBoxItem Tag="US" Content="US" />
                <ComboBoxItem Tag="CA" Content="CA" />
                <ComboBox.SelectedIndex>0</ComboBox.SelectedIndex>
            </ComboBox>

i want add this selected item to querystring in Model class, i am trying like below

Myclass myclass = new MyClass();     
QueryString.Add("SeletedItem", Convert.ToString(myclass.SelectedItem.Value));

here i am getting SelectedItem value as System.Web.ComboItem, but i want if i select 'US' as dropdown i need to get 'US'. how to get the value please can help me.

Upvotes: 1

Views: 565

Answers (2)

Bolu
Bolu

Reputation: 8786

Use QueryString.Add("SeletedItem",(myclass.SelectedItem.Value as System.Windows.Controls.ComboBoxItem).Content.ToString());

Upvotes: 0

daryal
daryal

Reputation: 14919

Instead of declaring combobox items in the xaml code, declare a them as an ObservableCollection in the model. Then bind this property to Items property of the combobox in the xaml.

Upvotes: 2

Related Questions