Hi,
I have a list of items in a XamGrid, and when editing one of the cells turns to a combobox, like this:
Now my question is, how do I populate this combobox ?
I have to classes, Brand and Account.
re relation between these to classes is many-to-one, where:
Brand have one account, and account can have many brands.
I was trying to populate it from code but comboBox is not visible somehow.
Hi Rob Stoffers,
Tnx for your reply,
I found out what was wrong. The binding is actually correct, and I do bind to the account, I just show the Name of the account on the text.
But the actual binding is to an Account object and not a string.
But the real problem was that the object was from to different sources, and of course even that they are the same object, they are not equal to each other in memory because they where from two different sources.
So I needed to create a converter to compare these two objects.
The modified code looks like this now.
<Page.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="..\..\..\Resources\PIMStylesDictionary.xaml" /> </ResourceDictionary.MergedDictionaries> <local:AccountList x:Key="AccountList"/> <local:AccountListConverter x:Key="AccountListConverter"/> </ResourceDictionary> </Page.Resources>
<iG:TemplateColumn Key="Account" HeaderText="Account" HorizontalContentAlignment="Stretch"> <iG:TemplateColumn.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Account.Name}"/> </DataTemplate> </iG:TemplateColumn.ItemTemplate> <iG:TemplateColumn.EditorTemplate> <DataTemplate> <iG:XamComboEditor x:Name="CbxAccounts" ItemsSource="{Binding Source={StaticResource AccountList}}" DisplayMemberPath="Name" SelectedItem="{Binding Account, Mode=TwoWay, Converter={StaticResource AccountListConverter}, ConverterParameter={StaticResource AccountList}}"/> </DataTemplate> </iG:TemplateColumn.EditorTemplate> </iG:TemplateColumn>
class AccountListConverter: IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var accounts = parameter as AccountList; if (accounts == null) return null; var selectedValue = (Account) value; return accounts.FirstOrDefault(account => account.AccountId == selectedValue.AccountId); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return value; } }
I am writing this in case any one else has same problem.
Thanks a lot for your help
Hi Nawed,
It's a little hard for me to tell exactly why it's not working since I don't have the same data and class objects that you are using but from what I can see through the bindings you have, it looks like you need to change your SelectedValue binding to "Account.Name". I say this because in your ItemTemplate you're binding the Text to "Account.Name" which tells me that Account is probably a class.
In the ComboBox, you are binding SelectedValue to that Account class but the SelectedValuePath is pointing to a string (the "Account" property in AccountList). The binding for SelectedValue needs to match the type used by the SelectedValuePath. Remember that SelectedValuePath is the path to a property in the ComboBox's ItemsSource.
Tnx for the reply,
I don't know what I am doing wrong but it doesn't work, still not showing the selected value when in edit mode.
Here is the code.
<iG:XamGrid x:Name="XdtgListOfAllBrands" AutoGenerateColumns="False" CellStyle="{StaticResource PIMCellStyle}"> <iG:XamGrid.Columns> <iG:TextColumn Key="Name" HeaderText="Brand"/> <iG:TemplateColumn Key="Account" HeaderText="Account" HorizontalContentAlignment="Stretch"> <iG:TemplateColumn.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Account.Name}"/> </DataTemplate> </iG:TemplateColumn.ItemTemplate> <iG:TemplateColumn.EditorTemplate> <DataTemplate> <ComboBox x:Name="CbxAccounts" ItemsSource="{Binding Source={StaticResource AccountList}}" SelectedValue="{Binding Path=Account}" SelectedValuePath="Account" DisplayMemberPath="Name"> </ComboBox> </DataTemplate> </iG:TemplateColumn.EditorTemplate> </iG:TemplateColumn> </iG:XamGrid.Columns> </iG:XamGrid>public Page GetBrandPageView() { _brandPageView.XdtgListOfAllBrands.ItemsSource = NavigationController.Instance.GetAllBrandsFromDb(); return _brandPageView; }
Please see the updated version of Marianne's sample I have attached. In order for the combobox to show the selected value you need to push the selection to the underlying item tied to the cell. You can use a binding for this. Bind the ComboBox.SelectedValue property to the property in the underlying datasource associated with the column.
sorry