ComBoxBox中的CheckBox和DataGrid中的数据排序

时间:2017-11-05 11:26:26

标签: c# wpf combobox datagrid ado.net

在ComboBox中有汽车品牌 Here is an example

这是xaml

<ComboBox ItemsSource="{Binding Brand}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox Content="{Binding Name}"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

并且在Datagrid中有车型

这是xaml

<DataGrid ItemsSource="{Binding Cars}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
                <DataGridTextColumn Header="Brand" Binding="{Binding Brands.Name}"/>
                <DataGridTextColumn Header="Model" Binding="{Binding Model}"/>
            </DataGrid.Columns>
        </DataGrid>

这是代码

private Brands selectedBrands;
        public ObservableCollection<Brands> Brand { get { return db.Brands.Local; } }
        public Brands SelectedBrand
        {
            get { return selectedBrands; }
            set
            {
                if (selectedBrands != value)
                {
                    selectedBrands = value;
                    OnPropertyChanged(nameof(SelectedBrand));
                    OnPropertyChanged(nameof(Cars));
                }
            }
        }
        public IEnumerable<Car> Cars
        {
            get
            {
                if (SelectedBrand != null)
                    return db.Car.Local.Where(c => c.Brand == SelectedBrand.Id);
                return null;
            }
        }

现在我需要使用ComboBox对数据进行排序,但我不知道如何。请给我一个例子或链接

1 个答案:

答案 0 :(得分:0)

从您的评论:如果在ComboBox中选择本田和现代,那么在DataGrid中只需要显示所选品牌的型号

然后以下工作,我以国家和城市为例

您需要从组合框选择更改事件中设置所选品牌并进行设置

public ObservableCollection<City> Cities {
  get;
  private set;
}

private Country _selectedCountry;
public Country SelectedCountry {
  get {
    return _selectedCountry;
  }
  set {
    _selectedCountry = value;
    this.Cities.Repopulate(_selectedCountry.Cities);
  }
}