0

在组合框中具有多项选择功能,因此在这种情况下如何将默认选定项目设置为所有选定的组合框。问题是如果我们使用下面的代码绑定然后选定的项目会影响所有组合框,所以请建议如何单独制作选定的项目。

组合框如下所示

![组合框如下所示,我需要绑定多个选定项目][1]

落下

![下拉菜单看起来像这样][2]

XAML

      <Window.Resources>
        <local:ViewModel x:Key="ComboSource" />
      </Window.Resources>
      <Grid DataContext="{StaticResource ComboSource}">
      <div:GridDataControl x:Name="DataGrid"
                                Grid.Row="1"
                                AutoPopulateColumns="False"
                                ItemsSource="{Binding GDCSource}"
                                ShowAddNewRow="False"
                                VisualStyle="Metro">
        <div:GridDataControl.VisibleColumns>

            <div:GridDataVisibleColumn MappingName="CustomerName">
                <div:GridDataVisibleColumn.CellItemTemplate>
                    <DataTemplate>
                        <div:ComboBoxAdv Name="customersCombo"
                                                AllowMultiSelect="True"
                                                DisplayMemberPath="ProductName"
                                                ItemsSource="{Binding Path=GDCSource,
                                                                      Source={StaticResource ComboSource}}"
                                                SelectedItems="{Binding Path=MySelectedItems,
                                                                        Source={StaticResource ComboSource}}"
                                                SelectedValuePath="ProductName"
                                                SelectionChanged="customersCombo_SelectionChanged" />
                    </DataTemplate>
                </div:GridDataVisibleColumn.CellItemTemplate>

            </div:GridDataVisibleColumn>

C# 代码

   public ViewModel()
    {
        this.GDCSource = Getsource();
        this.MySelectedItems = Getselected();
        this.Combosource = new ComboBoxSource();
    }

    private ObservableCollection<Data> _gdcsource;
    public ObservableCollection<Data> GDCSource
    {
        get { return _gdcsource; }
        set { _gdcsource = value; }
    }

    private List<string> _combosource;
    public  List<string> Combosource
    {
        get { return _combosource; }
        set { _combosource = value; }
    }

    private ObservableCollection<Data> _myselectedItems;
    public ObservableCollection<Data> MySelectedItems
    {
        get { return _myselectedItems; }
        set { _myselectedItems = value; }
    }

    private ObservableCollection<Data> Getselected()
    {
        var items = new ObservableCollection<Data>();
        if (items != null)
        {
            items.Add(GDCSource[0]);
            items.Add(GDCSource[1]);
        }
        return items;
    }
    private ObservableCollection<Data> Getsource()
    {
        ObservableCollection<Data> items = new ObservableCollection<Data>();
        if (items != null)
        {
            items.Add(new Data() { CustomerID = 1, CustomerName = "Thomas", ProductID 
              = 44, ProductName = "Reebok Shoes", ProductPrice = 200, Quantity = 4 });
            items.Add(new Data() { CustomerID = 2, CustomerName = "Johnson", ProductID 
            = 45, ProductName = "Adidas Shoes", ProductPrice = 150, Quantity = 5 });
        }
        return items;
           }}



     public class ComboBoxSource : List<string>
       {
        public ComboBoxSource()
        {
         this.Add("Thomas");
         this.Add("Johnson");
         this.Add("Vincent");
         this.Add("Jerald");
         this.Add("Peter");
         this.Add("David");
         this.Add("Peterson");
         this.Add("Robert");
         this.Add("Nestor");
        }
      }
4

1 回答 1

0

为了使每个ComboBox都有自己的SelectedItems,请移动类中的属性,Data因为 Data 是每个网格行的上下文。然后你可以绑定你的 SelectedITemsSelectedItems="{Binding Path=MySelectedItems}"

于 2013-09-20T17:02:44.657 回答