C# 将项目绑定到MVVM体系结构中的silverlight combobox
我正在实体类中创建一个属性,如下所示C# 将项目绑定到MVVM体系结构中的silverlight combobox,c#,wpf,silverlight,mvvm,C#,Wpf,Silverlight,Mvvm,我正在实体类中创建一个属性,如下所示 public class Myclass { private string _Selecteditem; public string SelectedItem { get{return _Selecteditem;} set{_Seleteditem = value; } } <ComboBox Name="cmbCountry" Grid.Column="14" Grid.Row="0" Width="150" SelectedItem="{Bin
public class Myclass
{
private string _Selecteditem;
public string SelectedItem
{
get{return _Selecteditem;}
set{_Seleteditem = value;
}
}
<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>
Myclass myclass = new MyClass();
QueryString.Add("SeletedItem", Convert.ToString(myclass.SelectedItem.Value));
在xaml页面中,我正在绑定组合框,如下所示
public class Myclass
{
private string _Selecteditem;
public string SelectedItem
{
get{return _Selecteditem;}
set{_Seleteditem = value;
}
}
<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>
Myclass myclass = new MyClass();
QueryString.Add("SeletedItem", Convert.ToString(myclass.SelectedItem.Value));
这里我得到的SelectedItem值为System.Web.ComboItem,但如果我选择“US”作为下拉列表,我需要得到“US”。如何获取值,请帮助我。不要在xaml代码中声明combobox项,而是在模型中将它们声明为
ObservableCollection
。然后将此属性绑定到xaml中组合框的Items
属性。使用QueryString.Add(“SeletedItem”,“myclass.SelectedItem.Value as System.Windows.Controls.ComboBoxItem”).Content.ToString()代码>