Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
49
xamNumericEditor issue
posted

We use a xamGrid, one of the columns is a double value which is defined with a mask:

 <ig:TemplateColumn Key="TransferRate" HeaderText="Transfer Rate" Width="96" IsResizable="False" HorizontalContentAlignment="Right" HeaderTextHorizontalAlignment="Right">

                    <ig:TemplateColumn.ItemTemplate>

                        <DataTemplate>

                            <TextBlock Text="{Binding TransferRate, Mode=OneWay, StringFormat=\{0:n6\}}"/>

                        </DataTemplate>

                    </ig:TemplateColumn.ItemTemplate>

                    <ig:TemplateColumn.EditorTemplate>

                        <DataTemplate>

                            <ig:XamNumericEditor Text="{Binding TransferRate}" Value="{Binding TransferRate, Mode=TwoWay}" Mask="{}{double:-4.6}" ValueChanged="XamNumericEditor_ValueChanged" PromptChar=" " GotFocus="XamNumericEditor_GotFocus" SelectionBackground="{StaticResource BlueSelectionBackgroundBrush}"/>

                        </DataTemplate>

                    </ig:TemplateColumn.EditorTemplate>

                </ig:TemplateColumn>

 

One of the requirements is: if the value is removed we should set the default value: 0.0 this was easily achieved using an converter or validating this in the ValueChanged event:

 public class TransferRateConverter : IValueConverter

    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

        {

            return value;

        }

 

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            if ( value == null || String.IsNullOrWhiteSpace(value.ToString().Trim()) || value.ToString().Trim().Equals(".") )

            {

                value = 0.0;

            }

            return value;

        }

    }

 

or

 

private void XamNumericEditor_ValueChanged(object sender, RoutedEventArgs e)

{

XamNumericEditor xne = sender as XamNumericEditor;

if (xne.Value == null || String.IsNullOrEmpty(xne.Text.Trim()) || xne.Text.Trim().Equals("."))

       {

            xne.Value = 0.0;

            xne.Text = "0.0";

       }

}

This is working fine, the problem appears when we remove the value and try to modify the same value again. Once we click into the column the text is selected and when you press any key number (let say 8) we can see this: 08.000000 and then you can type two more digits and you cannot overwrite the decimal part.

I tried to solve this manipulating the text value in the ValueChanged event doing something like:

private void XamNumericEditor_ValueChanged(object sender, RoutedEventArgs e)

{

XamNumericEditor xne = sender as XamNumericEditor;

string originalText = xne.Text != null ? xne.Text.Trim() : String.Empty;

bool intergetPart = true;

string result = String.Empty;

 

if (xne.Value == null || String.IsNullOrEmpty(xne.Text.Trim()) || xne.Text.Trim().Equals("."))

{

xne.Value = 0.0;

              xne.Text = "0.0";

}

 

if (xne.Text.ToString().StartsWith("0") && !xne.Text.Equals("0.000000"))

{

Char[] aux = xne.Text.ToArray();

foreach (char digit in aux)

{

if (digit.Equals('.') && originalText.Length > 4)

{

intergetPart = false;

                            result = String.Format("{0}{1}", result, '.');

}

if (intergetPart)

{

if (!digit.Equals(0))

                            {

                                result += digit;

                            }

}

else

                        {

result = String.Format("{0}{1}", result, originalText.Substring(5, originalText.Length - 5));

                            break;

                        }

                    }

                    if(String.IsNullOrEmpty(result))

                    {

                        result = "0.0";

                    }

                    xne.Text = result;

                    xne.Value = Convert.ToDouble(result);

                }

}

The result is again wrong, now I can see something like: 0.800000 and I can enter only one number.

Is there a way (somehow) to synchronize the text property and the value property? Or is there a way to set the text property exactly as we want (xne.Text =8 -> result 8)? Do I miss something?

Please see attached file for more information.


 

XamNumericEditor issue2.zip
Parents Reply Children
No Data