使用组合框过滤wpf ef

时间:2015-11-11 20:24:52

标签: c# wpf entity-framework combobox

我正在尝试使用多个组合框搜索表格,这些组合框绑定到工作站集合,每个组合框包含与工作站不同的“列名称”,例如:station.street,station.city等。这是我的代码示例: XAML:

<ComboBox Margin="0, 5" 
    ItemsSource="{Binding StationCollection, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="city"
    SelectedValue="{Binding Order_City, UpdateSourceTrigger=PropertyChanged}" 
    SelectedValuePath="city" 
    IsEditable="True"/>

<ComboBox Margin="0, 5" 
    ItemsSource="{Binding StationCollection, UpdateSourceTrigger=PropertyChanged}" 
    DisplayMemberPath="street" 
    SelectedValue="{Binding Order_Street, UpdateSourceTrigger=PropertyChanged}" 
    SelectedValuePath="street"/>

视图模型:

 public string Order_Street
 {
      get { return _order_street; }
      set
      {
          _order_street = value;
          OnPropertyChanged("Order_Street");
          GetStations();
      }
  }

  public string Order_City
  {
      get { return _order_city; }
      set
      {
          _order_city = value;
          OnPropertyChanged("Order_City");
          GetStations();
      }
   }

    private void GetStations()
    {
        using (var entities = new MobilekEntities())
        {
            var stationList = entities.Stations.AsEnumerable();

            if (!string.IsNullOrEmpty(_order_city))
            {
                stationList = stationList.Where(s => s.city.StartsWith(_order_city));
            } 

            if (!string.IsNullOrEmpty(_order_street))
            {
                stationList = stationList.Where(s => s.street.StartsWith(_order_street));
            } 

            if (!string.IsNullOrEmpty(_order_streetNumber))
            {
                stationList = stationList.Where(s => s.street.StartsWith(_order_streetNumber));
            }

            StationCollection = ToObservableCollectioncs.ToObservableCollection<Station>(stationList);
        }
    }

所以它很好用。它需要所有的站点并将它们写入组合框中,当我选择一个城市时,仅来自该城市的街道列在streetCombobox中。 但是,当我想改变一个城市时,只有我选择的城市。我让我的组合框可编辑,所以我想退格一些字母,然后列表会更新。例如:我选择旧金山,当我退回San时,圣何塞将出现在列表中。你有任何建议如何制作吗?

    public ObservableCollection<Station> StationCollection
    {
        get { return _stationCollection; }
        set { _stationCollection = value; OnPropertyChanged("StationCollection"); }
    }

0 个答案:

没有答案