首页 > 解决方案 > 更改文本后,绑定到 Run.TextProperty 会返回旧值

问题描述

我正在尝试创建一个内部对象,该对象的RichTextBox属性绑定到我的自定义对象中的字符串属性。这是我创建绑定的方式:RunText

Paragraph paragraph = new Paragraph();
Run run = new Run();
Binding bodyBinding = new Binding("Body");
PresentationTraceSources.SetTraceLevel(bodyBinding, PresentationTraceLevel.High);
bodyBinding.Source = OpenedFiles[index];
bodyBinding.Mode = BindingMode.TwoWay;
bodyBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
run.SetBinding(Run.TextProperty, bodyBinding);

paragraph.Inlines.Add(run);
FlowDocument flowDocument = new FlowDocument(paragraph);
RichTextBox rtb = new RichTextBox(flowDocument);

这是创建绑定时的调试信息:

System.Windows.Data Warning: 56  : Created BindingExpression (hash=27037160) for Binding (hash=32830290)
System.Windows.Data Warning: 58  : Path: 'Body'
System.Windows.Data Warning: 62  : BindingExpression (hash=27037160): Attach to System.Windows.Documents.Run.Text (hash=42007851)
System.Windows.Data Warning: 67  : BindingExpression (hash=27037160): Resolving source 
System.Windows.Data Warning: 70  : BindingExpression (hash=27037160): Found data context element: <null> (OK)
System.Windows.Data Warning: 78  : BindingExpression (hash=27037160): Activate with root item OpenedFile (hash=42526340)
System.Windows.Data Warning: 108 : BindingExpression (hash=27037160):   At level 0 - for OpenedFile.Body found accessor RuntimePropertyInfo(Body)
System.Windows.Data Warning: 104 : BindingExpression (hash=27037160): Replace item at level 0 with OpenedFile (hash=42526340), using accessor RuntimePropertyInfo(Body)
System.Windows.Data Warning: 101 : BindingExpression (hash=27037160): GetValue at level 0 from OpenedFile (hash=42526340) using RuntimePropertyInfo(Body): 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'
System.Windows.Data Warning: 80  : BindingExpression (hash=27037160): TransferValue - got raw value 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'
System.Windows.Data Warning: 89  : BindingExpression (hash=27037160): TransferValue - using final value 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'

这就是OpenedFile类的样子:

public class OpenedFile : INotifyPropertyChanged
{
    //Implementation of INotifyPropertyChanged interface 
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private string _name;
    private string _body;
    private string _prevbody;
    private string _path;
    private bool _modified;

    public string Name { get { return _name; } set { _name = value; NotifyPropertyChanged();} }
    public string Body {
        get { return _body; }
        set {
                _body = value;
                NotifyPropertyChanged();
            }
    }
    public string Path { get { return _path; } set { _path = value; NotifyPropertyChanged();} }
    public bool Modified { get { return _modified; } set { _modified = value; NotifyPropertyChanged(); } }
    public OpenedFile(string name, string body, string path)
    {
        Name = name;
        Body = body;
        Path = path;
        Modified = false;
    }

    public OpenedFile()
    {
        Name = "New File";
        Body = "";
        Path = "";
        Modified = true;
    }
}

此绑定一开始有效(RichTextBox 在创建时会显示正确的文件内容),但只要我在 RichTextBox 中输入内容,它就会给我以下信息(在此示例中,我在末尾输入了“x”)富文本框):

System.Windows.Data Warning: 90  : BindingExpression (hash=27037160): Update - got raw value 'nikjodfewsanioudawnuidwanuidwniuainudwsodfx'
System.Windows.Data Warning: 94  : BindingExpression (hash=27037160): Update - using final value 'nikjodfewsanioudawnuidwanuidwniuainudwsodfx'
System.Windows.Data Warning: 102 : BindingExpression (hash=27037160): SetValue at level 0 to OpenedFile (hash=42526340) using RuntimePropertyInfo(Body): 'nikjodfewsanioudawnuidwanuidwniuainudwsodfx'
System.Windows.Data Warning: 95  : BindingExpression (hash=27037160): Got PropertyChanged event from OpenedFile (hash=42526340)
System.Windows.Data Warning: 101 : BindingExpression (hash=27037160): GetValue at level 0 from OpenedFile (hash=42526340) using RuntimePropertyInfo(Body): 'nikjodfewsanioudawnuidwanuidwniuainudwsodfx'
System.Windows.Data Warning: 80  : BindingExpression (hash=27037160): TransferValue - got raw value 'nikjodfewsanioudawnuidwanuidwniuainudwsodfx'
System.Windows.Data Warning: 89  : BindingExpression (hash=27037160): TransferValue - using final value 'nikjodfewsanioudawnuidwanuidwniuainudwsodfx'
System.Windows.Data Warning: 90  : BindingExpression (hash=27037160): Update - got raw value 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'
System.Windows.Data Warning: 94  : BindingExpression (hash=27037160): Update - using final value 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'
System.Windows.Data Warning: 102 : BindingExpression (hash=27037160): SetValue at level 0 to OpenedFile (hash=42526340) using RuntimePropertyInfo(Body): 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'
System.Windows.Data Warning: 95  : BindingExpression (hash=27037160): Got PropertyChanged event from OpenedFile (hash=42526340)
System.Windows.Data Warning: 101 : BindingExpression (hash=27037160): GetValue at level 0 from OpenedFile (hash=42526340) using RuntimePropertyInfo(Body): 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'
System.Windows.Data Warning: 80  : BindingExpression (hash=27037160): TransferValue - got raw value 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'
System.Windows.Data Warning: 89  : BindingExpression (hash=27037160): TransferValue - using final value 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'

因此,它首先正确捕获更改并对其进行更新,然后将其恢复为旧值。此外,在那之后,绑定完全中断——它不会在任何文本输入上更新,但它也不会在调试中显示任何信息。

有谁知道如何修理它?拜托,我现在很绝望,我已经尝试调试了几个小时。我尝试从 OpenedFile 类中删除 INotifyPropertyChanged 实现并将绑定设置为不同的模式。

标签: c#wpfdata-binding

解决方案


推荐阅读