Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将项目绑定到MVVM体系结构中的silverlight combobox_C#_Wpf_Silverlight_Mvvm - Fatal编程技术网

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()