<Syncfusion:GridTemplateColumn MappingName="CountryCode" UseBindingValue="True"> <Syncfusion:GridTemplateColumn.DisplayBinding> <Binding Path="CountryCode"> <Binding.Converter> <local:DisplayMemberConverter DisplayMemberPath="CountryName" SelectedValuePath="CountryCode" DataContext="{StaticResource viewmodel}"/> </Binding.Converter> </Binding> </Syncfusion:GridTemplateColumn.DisplayBinding> <Syncfusion:GridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox x:Name="combobox" ItemsSource="{Binding Companies,Source={StaticResource viewmodel}}" SelectedValue="{Binding Path=CountryCode}" DisplayMemberPath="CountryName" SelectedValuePath="CountryCode" /> </DataTemplate> </Syncfusion:GridTemplateColumn.CellTemplate> |
public class DisplayMemberConverter : IValueConverter { public string DisplayMemberPath { get; set; } public string SelectedValuePath { get; set; } public object DataContext { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var selectedValue = value; IEnumerable list = null; var displayMemberPath = DisplayMemberPath; var valueMemberPath = SelectedValuePath;
list = (DataContext as ViewModel).Companies; if (selectedValue == null) return null;
PropertyDescriptorCollection pdc = null;
if (string.IsNullOrEmpty(valueMemberPath)) { if (!string.IsNullOrEmpty(displayMemberPath)) { var type = selectedValue.GetType();
pdc = TypeDescriptor.GetProperties(type);
return pdc.GetValue(selectedValue, displayMemberPath); } return selectedValue; } else { if (list == null) return null;
var enumerator = list.GetEnumerator(); while (enumerator.MoveNext()) { var type = enumerator.Current.GetType(); pdc = pdc ?? TypeDescriptor.GetProperties(type);
if (selectedValue.Equals(pdc.GetValue(enumerator.Current, valueMemberPath))) { if (!string.IsNullOrEmpty(displayMemberPath)) return pdc.GetValue(enumerator.Current, displayMemberPath); return enumerator.Current; } } } return null; }
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value; }
|
<Syncfusion:GridTemplateColumn MappingName="CountryCode" UseBindingValue="True"> <Syncfusion:GridTemplateColumn.ValueBinding> <Binding Path="CountryCode"> <Binding.Converter> <local:DisplayMemberConverter DisplayMemberPath="CountryName" SelectedValuePath="CountryCode" DataContext="{StaticResource viewmodel}"/> </Binding.Converter> </Binding> </Syncfusion:GridTemplateColumn.ValueBinding> <Syncfusion:GridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox x:Name="combobox" ItemsSource="{Binding Companies,Source={StaticResource viewmodel}}" SelectedValue="{Binding Path=CountryCode}" DisplayMemberPath="CountryName" SelectedValuePath="CountryCode" /> </DataTemplate> </Syncfusion:GridTemplateColumn.CellTemplate> |
Contacts | ||
idCon | Name | idCustomer |
1 | Marco Del Frate | 1 |
2 | Daniele Amato | 1 |
3 | Luciano Basteri | 1 |
4 | Giuseppe Benelli | 2 |
Customer | |
idCust | Name |
1 | Microsoft |
2 | Apple |
Name | Customer |
Marco Del Frate | Microsoft |
Daniele Amato | Microsoft |
Luciano Basteri | Microsoft |
Giuseppe Benelli | Apple |