c# Editable Text Block not retaining values

107 views Asked by At

I have an EditableTextBlock that has been working for some time in an existing application, however all of a sudden the behaviour of the EditableTextBLock has changed and is no longer retaining the new information on enter.

This is the XAML I am using

DataTemplate x:Key="percentageTemplate" >
        <AdornerDecorator>
            <platformControls:EditableTextBlock  Focusable="True"  Validation.ValidationAdornerSite="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListViewItem}}"                                                  
                                             Text="{Binding Path=Percentage, Mode=TwoWay, UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=True}" 
                                             KeyboardNavigation.DirectionalNavigation="Continue"                                                   
                                             KeyboardNavigation.IsTabStop="True"/>
        </AdornerDecorator>
    </DataTemplate>
    <DataTemplate x:Key="specTemplate">
        <AdornerDecorator>
            <platformControls:EditableTextBlock  Focusable="True"  Validation.ValidationAdornerSite="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListViewItem}}"                                                  
                                             Text="{Binding Path=SpecificGravity, Mode=TwoWay, UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=True}" 
                                             KeyboardNavigation.DirectionalNavigation="Continue"                                                   
                                             KeyboardNavigation.IsTabStop="True"/>
        </AdornerDecorator>
    </DataTemplate>

The problem as I see it is that is not getting to the setter, but is getting to the getter.

#region Property: SpecificGravity
    private double _specificGravity;

    public double SpecificGravity
    {
        get { return this._specificGravity; }
        set
        {
            if (value != 1)
            {
                IsModified = true;
            }

            _specificGravity = value;
            OnPropertyChanged("SpecificGravity");
        }
    }
    #endregion

When getting to this method below the textblock has the right data, but when the expression.UpdateTarget() runs, the value just returns back to its original value

 private static void IsInEditModeUpdate(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        EditableTextBlock textBlock = obj as EditableTextBlock;
        if (null != textBlock)
        {
            //Get the adorner layer of the uielement (here TextBlock)
            AdornerLayer layer = AdornerLayer.GetAdornerLayer(textBlock);

            //If the IsInEditMode set to true means the user has enabled the edit mode then
            //add the adorner to the adorner layer of the TextBlock.
            if (textBlock.IsInEditMode)
            {
                if (null == textBlock._adorner)
                {
                    textBlock._adorner = new EditableTextBlockAdorner(textBlock);
                    textBlock._adorner.Tag = textBlock;

                    //Events wired to exit edit mode when the user presses Enter key or leaves the control.
                    textBlock._adorner.TextBoxKeyUp += textBlock.TextBoxKeyUp;
                    textBlock._adorner.Focusable = true;
                    textBlock._adorner.TextBoxLostFocus += textBlock.TextBoxLostFocus;

                }
                layer.Add(textBlock._adorner);
            }
            else
            {
                //Remove the adorner from the adorner layer.
                Adorner[] adorners = layer.GetAdorners(textBlock);
                if (adorners != null)
                {
                    foreach (Adorner adorner in adorners)
                    {
                        if (adorner is EditableTextBlockAdorner)
                        {
                            layer.Remove(adorner);
                        }
                    }
                }

                //Update the textblock's text binding.
                BindingExpression expression = textBlock.GetBindingExpression(TextProperty);
                if (null != expression)
                {
                    expression.UpdateTarget();
                }                    
            }
        }
    }

The code that is used has been in this system for some time and looks like it originally came from here.

Code Project Link

The odd thing is percentageTemplate works as I would expect\want.

Any and all help appreciated.

Thanks

0

There are 0 answers