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;
}
In your combobox, you are binding to
Employeesin 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 datacontextThere 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)
Then in your datatemplate
Note that using a CollectionViewSource will allow you to do sort/group in xaml as well.