views:

12

answers:

1

I am DataTemplating a listbox's itemsource to display a label and combobox. In the, datatemplate I am assigning a new itemssource to the combobox but cant get it to work.

Or Ideally, how can I bind the combobox in a datatemplate to a different source.

Thanks. Mani

UserControl:

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

 <!--DataTemplate For SearchElement Type-->
<DataTemplate DataType="{x:Type CustomTypes:FilterElement}">
  <Label> Text </Label>
  *<ComboBox  ItemsSource="{Binding Employees}"DisplayMemberPath="Sex" />*
</DataTemplate>

ViewModel:

List<FilterElement> FilterControls;
List<Employee> Employees

Class FilterElement 
{
string Caption;
}

class Employee
{
string Sex;
}

+1  A: 

In your combobox, you are binding to Employees in the current data context, which would be a FilterElement object - no Employees property to bind to.

In your binding, you probably want to set Source= to something else, which overrides your datacontext

There are lots of ways to set this one easy way to do this (easy to put here, anyway) would be to add a collectionViewSource to the resources of your window/control (I put Whatever.Resources - it can go in nearly any containing element)

<Whatever.Resources>
  <CollectionViewSource x:Key="employeeSource" Source="{Binding Employees}">
</Whatever.Resources>

Then in your datatemplate

<ComboBox ItemsSource={Binding Source={StaticResource employeeSource}}" ... />

Note that using a CollectionViewSource will allow you to do sort/group in xaml as well.

Philip Rieck
This works. Thank you.
Mani