How can I set the DataTemplate on the XamComboEditor when its items are coming from a ComboBoxItemsProvider?
The items in the ComboBoxItemsProvider are objects with various properties. Say it contains ID and Name. How do I get the XamComboBox to display the ID and Name properties as one item? Eg. Instead of displaying names: "smith, brown, black", display IDs and names: "1 smith, 2 brown, 3 black"?
I'm aware that you can set the data template through ComboBoxStyle property but I'm having trouble with the binding.
<igEditors:XamComboEditor Name="cbo" ItemsProvider="{StaticResource cbip}">
<igEditors:XamComboEditor.ComboBoxStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=ID,
RelativeSource={RelativeSource FindAncestor,
AncestorType=igEditors:XamComboEditor}}"/>
<TextBlock Text="{Binding Path=Name,
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</igEditors:XamComboEditor.ComboBoxStyle>
</igEditors:XamComboEditor>
Since you want to show the Id & Name of your object (i.e. the DataContext), you should remove the RelativeSource otherwise you are trying to bind to properties named Id & Name on the XamComboEditor. If you still have an issue please post a small project showing the problem.
Thanks for your quick reply. My problem is actually that the XamComboBox is bound to a different datasource to the ComboBoxItemsProvider, therefore I need some way of binding to the ComboBoxItemsProvider's datacontext. I understand why what I have doesn't work and I have managed to get to the ItemsProvider's Items (see below) but to bind to the current item's properties in the provider is beyond me.
Is this possible?
<TextBlock Text="{Binding Path=ItemsProvider.Items.ID, RelativeSource={RelativeSource FindAncestor,
<TextBlock Text="{Binding Path=ItemsProvider.Items.Name, RelativeSource={RelativeSource FindAncestor,
Mm, I'll try to explain.
Say I have data from an AddressBook. This contains a property called CityID (but does not contain a CityName property).
I also have data from another object - Cities. This contains all the possible cities that could be selected for an Address and has a CityID and a CityName.
Now, my form which comes up displays the addresses. So my XamComboEditor shows the CityID for the address. That is, the editor itself is bound to the AddressBook data and its value is bound to CityID in AddressBook. This ensures that the initial selected value is the one supplied by the AddressBook data.
However, its ComboBoxItemsProvider is bound to the Cities data so that in the drop down menu it shows the list of all the other cities. This allows the user to change the City value in the address and have it propagate automatically to the AddressBook object.
<UserControl.Resources>
<igEditors:ComboBoxItemsProvider x:Key="cbipCities" DisplayMemberPath="CityID" ValuePath="CityID"/>
</UserControl.Resources>
...
<igEditors:XamComboEditor Name="xamCbo" ItemsProvider="{StaticResource cbipCities}" Value="{Binding CityID}" />
With the following pseudocode set in code behind:
cbipCities.ItemsSource = Cities;
xamCbo.DataContext = AddressBook;
So this is all well and good and works. My question is, is it possible to get the XamComboEditor's DataTemplate to display the CityID and CityName from the Cities data source (contained in ComboBoxItemsProvider and not in XamComboEditor) instead of just the CityID which is set through the DisplayMemberPath of the ComboBoxItemsProvider?
Or, if I managed to explain myself properly, would you suggest a different way of doing things? For sure it would be simpler to set the selected value of the XamComboEditor in code behind once the data has been loaded but then I will have to manually update any changes to the AddressBook object myself. Perhaps not a big deal but I have a lot of other data on the form which is automatically bound/updated. It doesn't seem right to have to manually do one of them.
Does the dropdown have to display the city id & name or can it just show the name? If it can just show the name then you can just change the DisplayMemberPath of the itemsprovider to CityName. If it needs to show both then you can try setting the ItemTemplate of the ComboBoxStyle of the XamComboEditor but a ComboBox will use the ItemTemplate for the edit area as well so while in edit mode you will see the itemtemplate and when out of edit mode, you will see the value represented by the displaymemberpath of the itemsprovider.
Good grief. I swear that's what I tried the first time and it couldn't find the data binding of the ItemsProvider because the combobox was bound to something else.
Using the ItemTemplate in the normal way under XamComboEditor.ComboBoxStyle works fine for me and is actually a bonus because it shows only the ID in the display from the XamComboEditor's datacontext but in the drop down it shows ID and Name from the ItemProvider's ItemsSource which is something else I wanted to accomplish. Speaking of which, is there a way to do that normally without databinding to two different sources?
Thanks for all your help!
Technically nothing has to be databound - you don't have to bind the edit portion or the dropdown - but then you need to manage the push/pull from the edit portion (i.e. the Value property) and populate the list in code. I'm not sure if that's what you mean by binding to two different sources.
Hmm, ok. Thanks.
It probably doesn't matter now but what I meant by databinding two sources was what I did in my above posts - binding the value to one source and the item list to another source. I think this is what you thought I meant.
Thanks for all your help.