0
votes

I have searched for a solution for a day and a half. Most examples that I get that explain / semi explain the situation relate to a combobox with "static" type items. I have the following structure.

  • DataModel Item

  • ObservableCollection CombinedData inside DataFilter class (i.e has other properties including "ParentName used for buttoncontent")

  • ObservableCollection Filters inside DataFilterGroup class

  • DataFilterGroup is 1 item in DataViewModel

So window's DataContext is DataViewModel.

I have a DataTemplate in which I would like to show the DataFilterGroup items from code behind

This is where I need help with please!!

so basically: ItemsControl's Itemsource is bound to DataFilterGroup

How do I bind a combobox in the DataFilter so that it's items source point to DataFilter. Therefore the source will change (or the content of the combobox will change with every DataFilterGroup Item).

I apologise if this is a repeat question. What I have up to now is the following (and have tried several ways to bind the combobox but no items appear. Supprisingly enough the Buttoncontent show up. Special combo is control deriverd from Combobox which is a button and combobox.

private static DataTemplate DataFilterTemplate
{
    get
    {
        DataTemplate DFT = new DataTemplate();
        DFT.DataType = typeof(DataFilters);

        FrameworkElementFactory Stack = new FrameworkElementFactory(typeof(VirtualizingStackPanel));
        Stack.SetValue(VirtualizingStackPanel.DataContextProperty, new Binding());
        Stack.SetValue(VirtualizingStackPanel.OrientationProperty, Orientation.Horizontal);

        FrameworkElementFactory Item = new FrameworkElementFactory(typeof(SpecialCombo));
        //Item.SetValue(SpecialCombo.DataContextProperty, new Binding());
        Item.SetValue(SpecialCombo.ButtonContentProperty, new Binding("ParentName"));

        Binding b = new Binding("CombinedData");

        //b.RelativeSource.AncestorType = typeof(ItemsControl);

        Item.SetBinding(SpecialCombo.ItemsSourceProperty, b);
        //Item.SetValue(SpecialCombo.ItemsSourceProperty, new Binding("CombinedData"));
        Item.SetValue(SpecialCombo.DisplayMemberPathProperty, "FullName");
        Item.SetValue(SpecialCombo.SelectedValuePathProperty, "Index");
        Item.SetValue(SpecialCombo.SelectedValueProperty, "SelectedItem");
        Item.SetValue(SpecialCombo.ToggleVisibleProperty, new Binding("ComboVisibility"));
        //Item.SetValue(SpecialCombo.SelectedValueProperty, new Binding("SelectedItem"));

        Stack.AppendChild(Item);

        DFT.VisualTree = Item;

        return DFT;
    }
}

Child is ItemsControl

Child.ItemsSource = DVM.Filters.FullCollection;
Child.ItemTemplate = DataFilterTemplate;
1

1 Answers

0
votes

Thanks to punker 76 in another post where I restructured the question (here -> Can one bind a combobox Itemssource from a datatemplate of a ItemsControl) slightly the following had to be done.

(1) The Object DataFilters had to change from [creation of Dependency Object]

public class DataFilters : INotifyPropertyChanged
// to
public class DataFilters : DependencyObject

(2) Observalbe collection should also change. so

public ObservableCollection<DataModel> CombinedData;
// should become
public static readonly DependencyProperty CombinedData= DependencyProperty.Register("CombinedData", typeof(ObservableCollection<DataModel>), typeof(DataFilters), new FrameworkPropertyMetadata());

//and 
public ObservableCollection<DataModel> CombinedData
{
    get { return (ObservableCollection<DataModel>)GetValue(CombinedDataProperty); }
    set { SetValue(CombinedDataProperty, value); }
}

(3) The correct Binding in the DataTemplate then becomes

Item.SetBinding(SpecialCombo.ItemsSourceProperty, new Binding("CombinedData") );

These are all the major steps to take to get a combobox type object be databound in a DataTemplate