Hello - We are evaluating your product and have a question regarding binding converters.
We have a ComboBox in our XamDataGrid. It's ItemSource is an observable collection of id/value pairs. The selected value of the ComboBox is bound to a property in our view model. Everything works fine if the bound property is a native string. However, if we attempt to bind to a property of a complex type using a converter, the value coming into the converter is always null.
Here is the XAML (ItemsSource is bound in code behind):
<igDP:ComboBoxField Name="CustomerId" Converter="{StaticResource idToStringConverter}" IsEditable="True" IsEnabled="True"> <igDP:ComboBoxField.EditorStyle> <Style TargetType="igEditors:XamComboEditor"> <Setter Property="ValuePath" Value="Id" /> <Setter Property="DisplayMemberPath" Value="DisplayName" /> </Style> </igDP:ComboBoxField.EditorStyle></igDP:ComboBoxField>
Here is our Converter:
public class IdToStringConverter : IValueConverter { public object Convert ( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) { return ( value is Id) ? ( ( Id ) value ).Value: null; } public object ConvertBack ( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) { return ( value is string ) ? new Id( value as string ): null; } }
Here is the property we are trying to bind to in our ViewModel:public Id CustomerId { set; get; }
When we bind to a native WPF DataGrid ComboboxColumn, it is simply this:
<DataGridComboBoxColumn SelectedValueBinding="{Binding Path=CustomerId, Converter={StaticResource idToStringConverter}}" SelectedValuePath="Id" DisplayMemberPath="DisplayName"/>
We have searched your forums and samples but have not found a straightforward answer.
Thank you
Update:
We solved our problem by using an alternate binding. But why is this necessary and/or why wouldn't we ALWAYS bind this way? Is there a downside?
We added this to our igDP:ComboBoxField:
AlternateBinding="{Binding Path=CustomerId, Mode=TwoWay, Converter={StaticResource idToStringConverter}}"
Hello Jeannine,
Thank you for following up. I agree that from my understanding the Converter property on the field should act the same way.
I plan on testing this and following up with my team if needed. If you happen to have a sample project that isolates this I'd greatly appreciate it if you can send it to me.
Let me know if you have any questions.
But is there any downside to using AlternateBinding, i.e. are there features of the XamDataGrid that won't work when we bind this way? I'm trying to understand what the purpose of this property is (other than to work around bugs).
AlternateBindings do have some additional memory and performance overhead.
I am currently in the middle of writing a sample to isolate this behavior and determine if it's a development issue or not. Please note we are closed Monday and Tuesday for the holidays. Let me know if you have any questions.