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
Hello,
I tried to reproduce this, but I was not able.
xmlns:sys="clr-namespace:System;assembly=mscorlib"...<sys:Int32 x:Key="int1">1</sys:Int32>...<igEditors:XamNumericEditor Value="{StaticResource int1}" ../>
Can you please provide more information about how you bind the XamNumericEditor?
Please note, that if the XamNumericEditor is in the XamDataGrid, the default value type would be double.
Regarding the Null values, the ValueEditors have a NullText property, which you can use to get this.
Regards,
Alex.
Thank you for your response.
I set the data context of my window to be an object that happens to have an integer property. I then bind the value of the editor to that integer property with respect to the data context (something like Value="{Binding Path=MyIntegerProperty}").
I tried using the NullText property but didn't have much success. Does it only work for nullable ints?
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?
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"
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?
With the Converter, it is not able to reproduce this, because when I clean the value from the XamNumericEditor, the converter returns Binding.DoNothing.
Sorry for the delayed response. Yes, when the text is cleared, it can't parse an int so it goes to Binding.DoNothing. My concern is that it looks like the data is cleared when it really isn't.
If I use the control as it is intended to be used (which I assume means that I need to set ValueType), is there any built in way to prevent this clearing confusion? I tried setting InvalidValueBehavior = "RevertValue" since I figured an empty value is invalid for an int and that it would revert the text back to what it was, but it didn't work.
Just to help anyone -- the above post works for null value text -- here's an example of what I've done that works for us:
<igEditors:XamNumericEditor Name="txtSpeed" Width="50" EditModeStarted="Numeric_EditModeStarted" ToolTip="RPMs must be between 200 and 3600." NullText="" Text="{Binding Path=EngineTests[0].Speed}" Value="{Binding Path=EngineTests[0].Speed}" Mask="nnnn" EditModeValidationError="txtSpeed_EditModeValidationError"> <igEditors:XamNumericEditor.ValueConstraint> <igEditors:ValueConstraint ValidateAsType="Integer32" MinInclusive="200" MaxInclusive="3600"></igEditors:ValueConstraint> </igEditors:XamNumericEditor.ValueConstraint> </igEditors:XamNumericEditor>
It's a pitty that xamNumericEditor doesn´t get the type of the bound property automatically. This means more work for developers each time a xamNUmericEditor is used in the form. Maybe in next release?
If you bind to Text property instead of Value, the set method of the property is triggered every time that the control gets or loses focus, or thw window is closed. If you bind to Value, the set method only is executed when the property actually changes.
I've found that if I both bind my properties to Text instead of Value and if I set Text = 0 when Text is null or empty after edit mode ends (via the event handler), then both of my problems are solved.
Thank you for all of your help with this.
I believe that has to be handled by the EditModeStarted/ing and EditModeEnded/ing events. As you have many editors, you can use the EventManager.RegisterClassHandler method do hook them up all at once, and perform this logic there (check for the value, conver value, set text if the ValueEditor has invalid value, etc..)