Hey,
I have a few questions related to the XamNumericEditor:
When I first tried binding an int to the XamNumericEditor I got the following error in the output:
System.Windows.Data Error: 22 : Cannot convert '1' from type 'Double' to type 'System.Int32' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: Int32Converter cannot convert from System.Double. at System.ComponentModel.TypeConverter.GetConvertFromException(Object value) at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)'
This goes away if I set the ValueType property to Int32. My question is: do I have to set ValueType or is it possible to have it automatically determined?
Also, when I completely clear the text in the XamNumericEditor (with ValueType=Int32), it does not set 0 in the underlying property. So, it looks like the value is empty, but it is still there. What is the best way to get around this?
Thanks
With the Converter, it is not able to reproduce this, because when I clean the value from the XamNumericEditor, the converter returns Binding.DoNothing.
Alex.
Thank you for your responses to my ValueType/conversion questions, they have been helpful.
Were you able to try out/reproduce my clearing -> value not setting issue?
The only thing that comes up in my mind is to use a Converter(IValueConverter) and check what the value is and manage this manually. However, you have to assign this manually next to the Binding Expression.
public class Converter1 : IValueConverter
{ public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int temp1;
double temp2;
if (value == null)
return Binding.DoNothing;
if (targetType == typeof(Int32))
if(int.TryParse(value.ToString(),out temp1))
return temp1;
if (targetType == typeof(Double))
if(double.TryParse(value.ToString(),out temp2))
return temp2;
}
#endregion
...
Value="{Binding Integer,Converter={StaticResource conv}}" NullText="this is null text"
Ahh, in my screens I have both double properties and int properties bound to XamNumericEditors so I don't think that this style will work for my case. I was hoping that there might be a setting where the control could figure out the type on the fly for the conversions.
I tried using NullText="0" but this is what happens: I type 111 into the editor and tab out of it. I put a break point in the my int property's setter. I go back to the XamNumericEditor and highlight all text and hit backspace to clear it and tab out of it. "0" shows in the editor, so it looks the way I want, but my setter is never hit so my integer property still has the value 111. Are you able to see this behavior as well?
You can create a style for the XamNumericEditor with no key, which will be automatically applied to all the XamNumericEditors like this:
<Style TargetType="{x:Type igEditors:XamNumericEditor}"><Setter Property="ValueType" Value="{x:Type sys:Int32}"/></Style>
....
<igEditors:XamNumericEditor Value="{Binding Integer}" NullText="this is null text" Name="xamNumericEditor1">