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次に、 SelectedITems を次のようにバインドできますSelectedItems="{Binding Path=MySelectedItems}"

于 2013-09-20T17:02:44.657 に答える