在更改文本后,绑定到Run.TextProperty会返回到旧值

时间:2018-05-28 10:44:07

标签: c# wpf data-binding

我正在尝试使用RichTextBox对象创建一个Run,其中Text属性绑定到我的自定义对象中的字符串属性。以下是我创建绑定的方法:

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中输入内容,它就会给我以下信息(在本例中,我键入了& #39; x&#39;在RichTextBox的末尾):

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实现并将绑定设置为其他模式。

0 个答案:

没有答案