Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This question has probably been asked several times. However all the posts I land on does not solve my problem. I have a DataModel that has an observable collection inside an observable collection. The visual tree should look something like ItemsControl->VirtualizingStack->Combobox. The following is simple example and "theoretically" items should appear in dropdown but it doesn't. Where am I going wrong.

XML
Public MainWindow()
     {
         InitializeComponent();

        ObservableCollection<Tester> Tester1 = new ObservableCollection<Tester>();
         Tester1.Add(new Tester("Sunny", "Jenkins"));
         Tester1.Add(new Tester("Pieter", "Pan"));

        ObservableCollection<Tester> Tester2 = new ObservableCollection<Tester>();
         Tester2.Add(new Tester("Jack", "Sanders"));
         Tester2.Add(new Tester("Bill", "Trump"));

        ObservableCollection<TheT> myDataV = new ObservableCollection<TheT>();
         myDataV.Add(new TheT(Tester1));
         myDataV.Add(new TheT(Tester2));

         IControl.ItemsSource = myDataV;
         IControl.ItemTemplate = TestingDT;
     }

    <ItemsControl x:Name="IControl" Margin="53,375,81,63">
         <ItemsControl.ItemsPanel>
             <ItemsPanelTemplate>
                 <VirtualizingStackPanel Orientation="Horizontal"/>
             </ItemsPanelTemplate>
         </ItemsControl.ItemsPanel>
     </ItemsControl>

    //the Datatemplate
     private DataTemplate TestingDT
     {
         get
         {
                 DataTemplate DFT = new DataTemplate();
                 DFT.DataType = typeof(TheT);

                FrameworkElementFactory Item = new FrameworkElementFactory(typeof(ComboBox));

                Binding B = new Binding("TesterObject")
                 {
                     Source = this
                 };

                 Item.SetBinding(ComboBox.ItemsSourceProperty, B);
                 //Item.SetValue(ComboBox.DisplayMemberPathProperty, "Name");


                 DFT.VisualTree = Item;
                 return DFT;
             }
         }

    //And the DATA Model
     class Tester
         {
             public Tester(string name, string surname)
             {
                 Name = name;
                 Surname = surname;
             }

            public string Name { get; set; }
             public string Surname { get; set; }
             public override string ToString()
             {
                 return Name + " " + Surname;
             }
         }

        class TheT
         {
             ObservableCollection<Tester> TesterObject;

            public TheT(ObservableCollection<Tester> testerObject)
             {
                 TesterObject = testerObject;
             }

            public string myDisplayName { get { return "test"; } }

            public void Add(Collection<Tester> col)
             {
                 TesterObject.Clear();
                 foreach (Tester t in col)
                { TesterObject.Add(t); }
             }
         }
Posted
Updated 9-Jul-13 3:59am
v3
Comments
Jan Lehmkuhl 9-Jul-13 14:19pm    
Does no one know how to bind a combobox ItemsSource from dataTemplate? Can it be done?

1 solution

Thanks to punker 76 in another post where I restructured the question (here -> http://stackoverflow.com/questions/17555458/can-one-bind-a-combobox-itemssource-from-a-datatemplate-of-a-itemscontrol) slightly the following had to be done.

(1) Normal class should become a dependencyobject therefore

public class TheT
// should become
public class TheT: DependencyObject


(2) Observalbe collection should also change to Dependency property. so

public ObservableCollection<tester> TesterObject;

// should become
public static readonly DependencyProperty TesterObject= DependencyProperty.Register("TesterObject", typeof(ObservableCollection<Tester>), typeof(TheT), new FrameworkPropertyMetadata());

//and should become a property
public ObservableCollection<Tester> TesterObject
{
get { return (ObservableCollection<Tester>)GetValue(TesterObjectProperty); }
set { SetValue(TesterObjectProperty, value); }
}

(3) Then in the DataTemplate file the following should change

Binding B = new Binding("TesterObject")
{
Source = this
};

Item.SetBinding(ComboBox.ItemsSourceProperty,B);
//Should change to
Item.SetBinding(SpecialCombo.ItemsSourceProperty, new Binding("CombinedData") );

I think all the above changes should be present in order to bind a combobox type item in a DataTemplate.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900