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.
I need to change the style and the template of the ComboBoxDataItem but I can not understand how it makes.Is possible to change the style and the template of the ComboBoxDataItem?
Thanks for reply
ComboBoxItem and ComboBoxDataItem are two separate things. ComboBoxDataItem is a class that may be used within things like a ComboBoxItemsProvider. When that object is put within a ContentControl such as a ComboBoxItem, the ContentPresenter within it will use the DataTemplate for that type to provide the content. The ComboBoxItem is the item container that the ComboBox itemscontrol will use to host each item. The Style for the ComboBoxItem is what is providing the highlight of the item so if you want to control that highlight in the dropdown then you would want to either put a Style for the ComboBoxItem or set the ItemContainerStyle in a setter for the ComboBoxStyle of the xamComboEditor to a style that does something different for the appearance of the comboboxitem (e.g. provides a different template that changes the appearance of the item when its IsHighlighted is true). If you use something like BamlViewer then you can look at the default styles for the comboboxitem. A basic example would be:
Or if you wanted to set the ComboBoxStyle:
Or since the default MS ComboBoxItem style changes the background to dynamic resources to systemcolor resourcekeys and you didn't mind that the highlight color would be used by the edit portion you could just put resources in comboeditor that use those keys. e.g.
I'm sorry, but here we talk about XamComboEditor and DataTemplate and I seemed the right place. However if I want to change color when it is highlighted, as I do? Can I use another type like ComboBoxItem? Is there a way to take ComboBoxItem automatically?
This might be better handled in a separate forum thread but basically that class is not an element so you need to define a DataTemplate whose DataType is igEditors:ComboBoxDataItem.
e.g.