Posted 7 August 2017, 8:46 am EST
Hi.
I work with the C1ComboBox. There I use the ItemConverter and in my ItemTemplate I also have a converter.
In some cases, which I can’t explain the converter don’t work.
Do you have any solution?
Regards
Forums Home / ComponentOne / Silverlight Edition
Posted by: patrick-muellner on 7 August 2017, 8:46 am EST
Posted 7 August 2017, 8:46 am EST
Hi.
I work with the C1ComboBox. There I use the ItemConverter and in my ItemTemplate I also have a converter.
In some cases, which I can’t explain the converter don’t work.
Do you have any solution?
Regards
Posted 7 August 2017, 8:46 am EST
Hello Patrick,
I’m sorry it is very difficult to comment on what could be causing the issue. It’ll be helpful if you can explain your scenario in detail and send across a sample so that I can also reproduce the issue and provide a solution for it.
Thanks
Abdias
Posted 7 August 2017, 8:46 am EST
I don’t have a sample I only can show you the code.
[xml]
<rza:RZAComboBox ItemConverter="{StaticResource SachkontenSteuersatzConverter}" SelectedItem="{Binding BuchungenClient.Buchung.Anzahlungserfassung.SteuersatzAnzahlung, Mode=TwoWay, ValidatesOnDataErrors=True}"
ItemsSource="{Binding BuchungenClient.Buchung.Anzahlungserfassung.MoeglicheSteuersaetze}" Width="100" HorizontalAlignment="Left" Grid.Row="3" Margin="1,2,1,2" Grid.Column="1"> <rza:RZAComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Satz, Converter={StaticResource SachkontenSteuersatzValueConverter}}"/> </DataTemplate> </rza:RZAComboBox.ItemTemplate> </rza:RZAComboBox>
[/xml]
[vb]
Public Class SachkontenSteuersatzValueConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert If value IsNot Nothing Then Dim dblTemp = CDbl(value) If dblTemp = 0 Then Return "-" Else Return dblTemp.ToString("N2", Globalization.CultureInfo.CurrentCulture) End If End If Return String.Empty End Function Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack Dim strValue As String = DirectCast(value, String) If strValue.Equals("-") Then Return 0 If String.IsNullOrEmpty(strValue) Then Return 0 Try Dim dclTemp = CDbl(strValue) If dclTemp <> Nothing Then Return Decimal.Parse(strValue, Globalization.CultureInfo.CurrentCulture) End If Catch ex As Exception Return 0 End Try Return 0 End Function
End Class
Public Class SachkontenKontonummerConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert Dim intTemp = CInt(value) If intTemp = 0 Then Return String.Empty End If Return intTemp.ToString End Function Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack Return String.Empty End Function
End Class
[/vb]
Posted 7 August 2017, 8:46 am EST
Hello Patrick,
The ItemConverter property of C1ComboBox requires an object of a class that inherits the TypeConverter class. Please take a look at the code below which shows the correct way to use ItemConverter :
[vb]Public Class SachkontenKontonummerConverter
Inherits TypeConverter
Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As Globalization.CultureInfo, value As Object, destinationType As Type) As Object If value Is Nothing Then Return String.Empty Else Return CType(value, Test).Id.ToString() End If End Function Public Overrides Function ConvertFrom(context As ITypeDescriptorContext, culture As Globalization.CultureInfo, value As Object) As Object Return String.Empty
End Function
End Class[/vb]
[xml]<local:SachkontenKontonummerConverter x:Key="itemConverter"></local:SachkontenKontonummerConverter>
<c1:C1ComboBox x:Name="c1ComboBox1" HorizontalAlignment="Left" Margin="45,52,0,0" VerticalAlignment="Top" Width="166" Height="22"
ItemsSource="{Binding ItemsList}" SelectedItem="{Binding TestSelectedItem}"
ItemConverter="{StaticResource itemConverter}" >[/xml]
I’ve also attached a sample application for your reference. Please take a look.
Let me know if you need any other help.
Regards
Abdias
Posted 7 August 2017, 8:46 am EST
Sorry I copy the wrong code. Sometimes it works and sometimes not.
Here once more my code:
[vb]
Public Class SachkontenSteuersatzConverter
Inherits TypeConverter
Public Overrides Function ConvertTo(context As System.ComponentModel.ITypeDescriptorContext, culture As System.Globalization.CultureInfo, value As Object, destinationType As System.Type) As Object If value IsNot Nothing Then Dim objSteuersatz = TryCast(value, ISteuersatz) If objSteuersatz IsNot Nothing Then If objSteuersatz.Satz = 0 Then Return "-" Else Return objSteuersatz.Satz.ToString("N2") End If End If End If Return String.Empty End Function Public Overrides Function ConvertFrom(context As System.ComponentModel.ITypeDescriptorContext, culture As System.Globalization.CultureInfo, value As Object) As Object 'If TypeOf value Is Double Then ' Return CDbl(value) 'Else ' Return Nothing 'End If Return String.Empty End Function
End Class
Public Class SachkontenSteuersatzValueConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert If value IsNot Nothing Then Dim dblTemp = CDbl(value) If dblTemp = 0 Then Return "-" Else Return dblTemp.ToString("N2", Globalization.CultureInfo.CurrentCulture) End If End If Return String.Empty End Function Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack Dim strValue As String = DirectCast(value, String) If strValue.Equals("-") Then Return 0 If String.IsNullOrEmpty(strValue) Then Return 0 Try Dim dclTemp = CDbl(strValue) If dclTemp <> Nothing Then Return Decimal.Parse(strValue, Globalization.CultureInfo.CurrentCulture) End If Catch ex As Exception Return 0 End Try Return 0 End Function
End Class
[/vb]
Posted 7 August 2017, 8:46 am EST
Hello Patrick,
Could you please elaborate “Sometimes it works and sometimes not”? Do you get an error or the values don’t get displayed correctly? It’ll be helpful if you could attach a screenshot. Also, could you please try the same code in the sample I had provided earlier?
Thanks
Abdias
Posted 7 August 2017, 8:46 am EST
Exactly.
I’ve choose the SelectedItem “0”. So on the display should be “-”. Sometimes it works fine and sometimes there is “0,00” displayed.
I don’t have any sample. Becuase in the most cases it works. It doesn’t works if I want to edit the window where the ComboBox is inside.
Regards
Posted 7 August 2017, 8:46 am EST
Hello Patrick,
Unfortunately, I’m still not able to reproduce the issue with the code you provided.
“It doesn’t works if I want to edit the window where the ComboBox is inside”
Have you placed the ComboBox inside a ChildWindow? I tried putting the ComboBox in a ChildWindow as well but could not reproduce the issue.
I’ve attached the sample application and a gif file for your reference. Please take a look and confirm if the issue occurs at your end.
Let me know if I’m missing anything
Regards
Abdias
2015/03/C1ComboBox_SL_ItemConverter_ChildWindow.zip
Posted 4 February 2018, 11:39 pm EST
First of all, sincere apologies for not being able to get back to you earlier. Our website went through a major upgrade sometime last year and this one got lost somewhere.
It is really difficult to replicate the issue with just the code you’ve sent. Can you please modify the sample application I sent earlier and see if you can replicate the issue with it and send it back to us.
Thanks