开发者

With linked ComboBoxes, secondary combobox always starts empty

I've been trying for two days now to figure this out, and repeatedly searching for answers. It seems like it should be so simple.

The goal: I have two comboboxs. One is for an object Country, the other is for CountrySubdivision. (EG USA has States, Canada has Provinces, etc.) I want to load the form and have both the Country and CountrySubdivision in place.

Project has a Country and CountrySubdivions. Country has an ObservableCollection of CountrySubdivision. All objects are INotifyCollectionChanged. So here's where it gets irritating. The bindings "work." In that, if I change the values and save, the proper values are put back into the database. However, every time I open up the application, CountrySubdivision starts empty. The backing value is still set, but the combo box isn't set.

Here are the applicable bindings:

<ComboBox
    x:Name="uiProjectCountrySubdivisions"
    DisplayMemberPath="Name"
    SelectedItem="{Binding Project.ProjectCountrySubdivision}"
    ItemsSource="{Binding Project.ProjectCountry.CountrySubdivisions}"
    FontSize="10.667" />

<ComboBox
    x:Name="uiProjectCountry"
    SelectedItem="{Binding Project.ProjectCountry}"
    ItemsSource="{Binding Countries}"
    DisplayMemberPath="Name"
    FontSize="10.667" />

And as requested, here are the (severly shorted) classes.

public class Project : BaseNotifyPropertyChanged
{
  private Guid _projectId;
  public virtual Guid ProjectId
  {
    get { return _projectId; }
    set
    {
      if (_projectId != value)
      {
        _projectId = value;
        OnPropertyChanged(() => ProjectId);
      }
    }
  }

  private string _projectName;
  public virtual string ProjectName
  {
    get { return _projectName; }
    set
    {
      if (_projectName != value)
      {
        _projectName = value;
        OnPropertyChanged(() => ProjectName);
      }
    }
  }

  private string _projectNumber;
  public virtual string ProjectNumber
  {
    get { return _projectNumber; }
    set
    {
      if (_projectNumber != value)
      {
        _projectNumber = value;
        OnPropertyChanged(() => ProjectNumber);
      }
    }
  }

  private string _projectAddressLineOne;
  public virtual string ProjectAddressLineOne
  {
    get { return _projectAddressLineOne; }
    set
    {
      if (_projectAddressLineOne != value)
      {
        _projectAddressLineOne = value;
        OnPropertyChanged(() => ProjectAddressLineOne);
      }
    }
  }

  private string _projectAddressLineTwo;
  public virtual string ProjectAddressLineTwo
  {
    get { return _projectAddressLineTwo; }
    set
    {
      if (_projectAddressLineTwo != value)
      {
        _projectAddressLineTwo = value;
        OnPropertyChanged(() => ProjectAddressLineTwo);
      }
    }
  }

  private string _projectCity;
  public virtual string ProjectCity
  {
    get { return _projectCity; }
    set
    {
      if (_projectCity != value)
      {
        _projectCity = v开发者_运维知识库alue;
        OnPropertyChanged(() => ProjectCity);
      }
    }
  }

  private string _projectZip;
  public virtual string ProjectZip
  {
    get { return _projectZip; }
    set
    {
      if (_projectZip != value)
      {
        _projectZip = value;
        OnPropertyChanged(() => ProjectZip);
      }
    }
  }

  private Country _projectCountry;
  public virtual Country ProjectCountry
  {
    get { return _projectCountry; }
    set
    {
      if (_projectCountry != value)
      {
        _projectCountry = value;
        OnPropertyChanged(() => ProjectCountry);
      }
    }
  } 

  private CountrySubdivision _projectCountrySubdivision;
  public virtual CountrySubdivision ProjectCountrySubdivision
  {
    get { return _projectCountrySubdivision; }
    set
    {
      if (_projectCountrySubdivision != value)
      {
        _projectCountrySubdivision = value;
        OnPropertyChanged(() => ProjectCountrySubdivision);
      }
    }
  }
}  

public class Country : BaseNotifyPropertyChanged
{
  private int _id;
  public virtual int CountryId
  {
    get { return _id; }
    set
    {
      if (_id != value)
      {
        _id = value;
        OnPropertyChanged(() => CountryId);
      }
    }
  }

  private string _name;
  public virtual string Name
  {
    get
    {
      return _name;
    }
    set
    {
      if (_name != value)
      {
        _name = value;
        OnPropertyChanged(() => Name);
      }
    }
  }

  private string _code;
  public virtual string Code
  {
    get
    {
      return _code;
    }
    set
    {
      if (_code != value)
      {
        _code = value;
        OnPropertyChanged(() => Code);
      }
    }
  }

  private ObservableCollection<CountrySubdivision> _countrySubdivisions = new ObservableCollection<CountrySubdivision>();
  public virtual ObservableCollection<CountrySubdivision> CountrySubdivisions
  {
    get
    {
      return _countrySubdivisions;
    }
    set
    {
      if (_countrySubdivisions != value)
      {
        _countrySubdivisions = value;
        OnPropertyChanged(() => CountrySubdivisions);
      }
    }
  }

  private string _subdivisionTypeShortDescription;
  public virtual string SubdivisionTypeShortDescription
  {
    get
    {
      return _subdivisionTypeShortDescription;
    }
    set
    {
      if (_subdivisionTypeShortDescription != value)
      {
        _subdivisionTypeShortDescription = value;
       OnPropertyChanged(() => SubdivisionTypeShortDescription);
      }
    }
  }

  private string _subdivisionTypeLongDescription;
  public virtual string SubdivisionTypeLongDescription
  {
    get
    {
      return _subdivisionTypeLongDescription;
    }
    set
    {
      if (_subdivisionTypeLongDescription != value)
      {
        _subdivisionTypeLongDescription = value;
        OnPropertyChanged(() => SubdivisionTypeLongDescription);
      }
    }
  }
}

public class CountrySubdivision : BaseNotifyPropertyChanged
{
  private int _id;
  public virtual int CountrySubdivisionId
  {
    get { return _id; }
    set
    {
      if (_id != value)
      {
        _id = value;
        OnPropertyChanged(() => CountrySubdivisionId);
      }
    }
  }

  private string _name;
  public virtual string Name
  {
    get { return _name; }
    set
    {
      if (_name != value)
      {
        _name = value;
        OnPropertyChanged(() => Name);
      }
    }
  }

  private Country _country;
  public virtual Country Country
  {
    get { return _country; }
    set
    {
      if (_country != value)
      {
        _country = value;
        OnPropertyChanged(() => Country);
      }
    }
  }
}

public abstract class BaseNotifyPropertyChanged : INotifyPropertyChanged
{
  public virtual event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanged(Expression<Func<object>> propertySelector)
  {
    //Stuff that turns () => Property into a fired Property Changed event.
  }
}

Please help!!


Is the SelectedItem on your ComboBox an object? If so, is it the exact same instance that exists in the "Countries" collection? Remember that for WPF to consider something "Selected" it must be able to find the "SelectedItem" in the "Countries" collection. If these types differ or instances differ that won't work.

I prefer binding my ComboBoxes as follows:

IDictionary<int, string> Countries
int? CountryId

<ComboBox
  ItemsSource="{Binding Countries}"
  SelectedValue="{Binding CountryId}"
  SelectedValuePath="Key"
  DisplayMemberPath="Value"/>

Still, as Alex asks, please list the relevant class definitions and relevant properties too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜