如何在Grid控件的datatemplate中绑定

时间:2013-09-20 16:25:13

标签: c# wpf binding combobox datatemplate

组合框中的

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

组合框如下所示

![组合框看起来如下,我需要绑定多个选定的项目] [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");
        }
      }

1 个答案:

答案 0 :(得分:0)

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