wpf组合框SelectedValue = null问题

时间:2014-06-25 20:44:51

标签: c# wpf combobox

我有一个wpf格式的组合框 我将ItemSource设置为Dictionary of Pet(宠物类型)的集合,只显示Value并隐藏Key

public void BindComboBoxes()
{
    this.cboTypes.ItemsSource = new BindingSource(CommonMgr.GetPetTypesDropDown(false), null);
    this.cboTypes.DisplayMemberPath = "Value";
    this.cboTypes.SelectedValuePath = "Key";
}

然后每当我键入以编码新的Breed对象,并在其项目中不存在的某些内容的cboTypes中键入文本时(不在数据库中),我的程序将询问最终用户是否要添加该文本数据库中的新PetType,如果是,那么它将会这样做。

然后我再次使用BindComboBoxes方法更新cboTypes,将cboTypes.Text设置为新项目并将Key分配给指定字段,但问题是,它是null。虽然它在Windows窗体中运行良好。这是我的代码:

public Breed GetPageEntity()
{
    Breed setEntity = new Breed();
    bool doesExist = false;
    setEntity.Id = DefaultValue.GetInt(this.txtId.Text);
    setEntity.BreedName = DefaultValue.GetString(this.txtName.Text);


    try
    {
        setEntity.PetTypeId = DefaultValue.GetInt(this.cboTypes.SelectedValue.ToString());
    }
    catch (Exception)
    {
        var addAnother = MessageBox.Show(String.Format("{0}: This type is not in the database. \nAdd {0} to the database?",
            this.cboTypes.Text), "Pet Type Cannot Be Found", MessageBoxButtons.OKCancel);

        if (addAnother == System.Windows.Forms.DialogResult.OK)
        {
            petTypeMgr.Entity = this.PetTypeAdder(cboTypes.Text);
            string temp = this.cboTypes.Text;
            petTypeMgr.Insert((petTypeMgr.Entity), fUser.Entity.Id, ref doesExist);
            //cboTypes.ItemsSource = null;
            //cboTypes.Items.Clear();
            BindComboBoxes();
            cboTypes.Text = temp;

            //SelectedValue became null
            setEntity.PetTypeId = DefaultValue.GetInt(this.cboTypes.SelectedValue);
        }

    }

    setEntity.Description = DefaultValue.GetString(this.txtDescription.Text);
    setEntity.SortOrder = DefaultValue.GetInt(txtSortOrder.Text);
    setEntity.StatusId = true;
    return setEntity;
}

2 个答案:

答案 0 :(得分:0)

如果数据绑定到后面代码中的属性,您会发现它更容易:

// Implement INotifyPropertyChanged interface properly here

private Dictionary<string, Pet> yourProperty = new Dictionary<string, Pet>();

public Dictionary<string, Pet> YourProperty
{
    get { return yourProperty; }
    set
    {
        yourProperty = value;
        NotifyPropertyChanged("YourProperty");
    }
}

private KeyValuePair<string, int> yourSelectedProperty;

public KeyValuePair<string, int> YourSelectedProperty
{
    get { return yourSelectedProperty; }
    set
    {
        yourSelectedProperty = value;
        NotifyPropertyChanged("YourSelectedProperty");
    }
}

然后在XAML中:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <ComboBox ItemsSource="{Binding YourProperty}" DisplayMemberPath="Value"
    SelectedValuePath="Key" SelectedItem="{Binding YourSelectedProperty}" />
    <TextBlock Grid.Column="1" Text="{Binding YourSelectedProperty.Key}" />
</Grid>

您只需要像这样设置ItemsSource一次。一旦数据绑定到集合属性,您就可以对集合进行更改,它们将自动在UI中更新。因此,假设您的GetPetTypesDropDown方法返回正确的类型,您应该能够像这样更新ComboBox项:

YourProperty = CommonMgr.GetPetTypesDropDown(false);

或者,你可以同样做这样的事情来更新它:

YourProperty = new Dictionary<string, int>();
foreach (YourDataType dataType in CommonMgr.GetPetTypesDropDown(false))
{
    YourProperty.Add(dataType.Key, dataType.Value);
}

答案 1 :(得分:0)

为什么不把Breed绑在Combobox上?

在Breed类中重写ToString()方法,以便该框显示您想要的内容。

class Breeds
{
 //Variables
 public void override ToString()
 {
   return Breedname;
 }
}

设置组合框

List<Breeds> breedlist = new List<Breeds>();
this.cboTypes.ItemsSource = breedlist;

阅读组合框

if(cboTypes.SelectedItem != null)
{
 Breeds breed = (Breeds)cboTypes.SelectedItem;
//Do stuff
}
else
{
 //Create new breed
}