views:

42

answers:

2

I am DataTemplating a listbox's itemsource to display a series of comboboxes. I want to give the 'DisplayMemberPath' of the combo to a property, which is in a different source than its own 'ItemsSource'. (Assuming DisplayMemberPath is just a string representing name of a property, I am getting this from the user). I have achieved this with a CollectionViewSource, but all the comboboxes are displaying the same list.

What I am expecting to have after data templating is to have comboboxes display,

ComboboxInstance1.DisplayMemberPath =  PropertyMapOfEmployee in FilterControls[0]
ComboboxInstance2.DisplayMemberPath =  PropertyMapOfEmployee in FilterControls[1]

Is this possible to achieve in XAML ?

Thanks. Mani

UserControl:

<Resources>
    <CollectionViewSource x:Key="bindingSource" Source="{Binding BindingItems}"/>
    <CollectionViewSource x:Key="FilterSource" Source="{Binding FilterControls}"/>

 <DataTemplate DataType="{x:Type CustomTypes:FilterElement}">
      <ComboBox ItemsSource="{Binding Source={StaticResource bindingEmp}" 
   DisplayMemberPath="{Binding Source={StaticResource FilterSource}, Path=PropertyMapofEmployee}" />
  </DataTemplate>

  <Resources>

        -------------

            <DockPanel>
                    <ListBox x:Name="lstBox" ItemsSource="{Binding FilterControls}" />
            </DockPanel>

ViewModel:

List<FilterElement> FilterControls;
List<Employee> Employees

class FilterElement 
{
string Caption;
String PropertyMapofEmployee
}
+2  A: 
<ComboBox ItemsSource="{Binding Source={StaticResource bindingEmp}"
          DisplayMemberPath="{Binding PropertyMapofEmployee}" />
Thomas Levesque
I don't think PropertyMapOfEmployee is a property on the Employee class so I don't think this will work.
Dave White
Thanks .. this actually fixes both the original and current issue.
Mani
It works because PropertyMapOfEmployee is a proprty of FilterControls which is binded to thge ListBox (which is datatemplated).Hence the combo bindings always refers to the parent, unless specifically set using 'Source=' in the binding. Hence it works.
Mani
@Dave, the Employee object is not the DataContext of the ComboBox, it's the DataContext of the ComboBoxItem...
Thomas Levesque
A: 

I'm not sure you can do that in XAML. (Having the DisplayMemberPath point to a property that is on an object other than the DataContext). You may want to look at the RelativeSource Class to see if that would meet your needs.

Have you thought about providing a reference in your Employee object to the FilterElement and then hooking up to the binding the Employee.PropertyMapOfEmployee property that you've created?

Dave White
It is possible. see below.
Mani