wpf组合框SelectedValue = null问题

本文关键字:null 问题 SelectedValue 组合 wpf | 更新日期: 2023-09-27 18:10:14

我得到了一个wpf形式的组合框我将ItemSource设置为(宠物类型)Dictionary的集合,只显示值并隐藏键

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

然后,每当我键入编码一个新的品种对象,并在其项目(不在数据库中)中不存在的东西的cboTypes中键入文本时,我的程序将询问最终用户是否想要在数据库中添加新的PetType,如果是,那么它将这样做。

然后我再次使用BindComboBoxes方法更新cboTypes,设置cboTypes。文本输入到新项中,并将Key分配给指定的字段,但问题是,它说,它是空的。不过它在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;
}

wpf组合框SelectedValue = null问题

如果您在后面的代码中将数据绑定到属性,您会发现这要容易得多:

// 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);
}

为什么不直接将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
}