c#可编辑文本块不保留值

时间:2015-06-26 15:20:10

标签: c# textblock adorner

我有一个EditableTextBlock,它已经在现有应用程序中工作了一段时间,但是EditableTextBLock的行为突然改变了,并且不再保留enter上的新信息。

这是我正在使用的XAML

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>

我看到的问题是,没有进入设定者,而是进入了吸气剂。

#region Property: SpecificGravity
    private double _specificGravity;

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

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

当在下面找到此方法时,textblock具有正确的数据,但是当expression.UpdateTarget()运行时,该值只返回其原始值

 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();
                }                    
            }
        }
    }

使用的代码已经在这个系统中使用了一段时间,看起来它最初来自这里。

Code Project Link

奇怪的是,百分比模板的工作方式正如我所期望的那样。

任何和所有帮助表示赞赏。

由于

0 个答案:

没有答案