Question

I have a datagrid where user enter values. Here is one column:

<datagrid:ThemedDataGrid   AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="True" ItemsSource="{Binding Model.ItemCollection, UpdateSourceTrigger=PropertyChanged}"  
                             SelectionUnit="FullRow"  
                             SelectedItem="{Binding Model.SelectedItem}" >
                   <datagrid:ThemedDataGrid.Columns >
                       <datagrideditors:DataGridDoubleColumn  Binding="{Binding OFFSET, UpdateSourceTrigger=PropertyChanged}" Header="{DynamicResource Offset}" />
                   </datagrid:ThemedDataGrid.Columns>
    </datagrid:ThemedDataGrid>

I need to allow user to enter only 5 digids in fractional part. So I've made property OFFSET this way :

public new double OFFSET 
{
    get { return _offset; }
    set
    {
        int count = 0;
        bool isSeparator = false;
        char a = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
        var formatted = value.ToString().ToCharArray();
        foreach (var y in formatted)
        {
            if (!isSeparator)
            {
                if (y == a)
                {
                    isSeparator = true;
                }
            }
            else
            {
                count++;
            }
        }
        if (isSeparator)
        {
            if (count > 5)
            {
                return;
            }
        }
        _offset = value;
        OnPropertyChanged();
        }
    }

User is not allowed to enter the sixth digit in fractional part, and here user can enter as much digits as he wants, and only when he entered value,fractional value changes to value with 5 digits in fractional part.

How to fix this?

Was it helpful?

Solution

I get English may be a second language but this is not clear

User is not allowed to enter the sixth digit in fractional part, and here user can enter as much digits as he wants, and only when he entered value,fractional value changes to value with 5 digits in fractional part.

Consider

if (count > 5) { return; }

You have done nothing to the value in the DataGrid
You need to assign to _offset the proper value and call OnPropertyChanged();

For example

if (count > 5) 
{ 
    _offset = 5.55555;
    // I leave it to you to properly assign _offset;
    OnPropertyChanged();
    return; 
}

    StringBuilder() sb = new StringBuilder();
    foreach (var y in formatted)
    {

        if (!isSeparator)
        {
            if (y == a)
            {
                isSeparator = true;
            }
        }
        else
        {
            count++;
            if (count > 5) break;
        }
        sb.Append(y);
    }
    if (isSeparator)
    {
        if (count > 5)
        {
            _offset = Double.Parse(sb.ToString());
            OnPropertyChanged();
            return;
        }
    }

And if you are on .NET 3.5 or 4.0 it then you might have a problem with the UI not updating.
But on .NET 4.5 it works fine for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top