WPF FlowDocumentScrollViewer文档无法操纵/绑定到

时间:2018-08-06 12:08:29

标签: c# wpf data-binding flowdocument

我要实现的目标
我想在运行时更改文本特定部分的颜色

先前的解决方案尝试
使用FlowDocumentScrollViewer [1]和对生成的FlowDocument [2]的绑定=> "The calling thread cannot access this object because a different thread owns it."

使用扩展的WPF工具包RichTextBox绑定到生成的FlowDocument =>此控件实际上允许我绑定到FlowDocument并正确显示它,但是每当我尝试更改[3] FlowDocument中的任何内容时:{{1 }}

我开始用"The calling thread cannot access this object because a different thread owns it."包装一些语句 不幸的是,这也没有帮助。

其余问题:
为什么其他线程拥有此对象?
如何避免这种情况?

编辑:原来是我尝试绑定到“ SelectedMapping”属性,该属性也绑定到组合框的“ SelectedValue”属性。

[1]

"Application.Current.Dispatcher.Invoke"

[2]

<FlowDocumentScrollViewer
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    Document="{Binding SelectedMapping.OriginalCharacterSequence.RichTextContent">
</FlowDocumentScrollViewer>

[3]

var sequenceString = "53QMDIONORMPALACEBIO"; 
var run = new Run(sequenceString);
var paragraph = new Paragraph(run);
RichTextContent = new FlowDocument(paragraph); // The property for the document binding  

也因为在每种情况下使用:SelectedMapping

 private void EditTextByIndex(int startOfText, int endOfText, Brush newColor)
    {
        var textRangeOfDocument = new TextRange(SelectedMapping.OriginalCharacterSequence.StartPointer, SelectedMapping.OriginalCharacterSequence.EndPointer);

        var startOfTextPointer = textRangeOfDocument.Start.GetPositionAtOffset(startOfText, LogicalDirection.Forward);

        var endOfTextPointer = textRangeOfDocument.Start.GetPositionAtOffset(endOfText, LogicalDirection.Backward);

        var textRangeOfTarget = new TextRange(startOfTextPointer, endOfTextPointer);

        System.Windows.Application.Current.Dispatcher.Invoke(() =>
        {
            textRangeOfTarget.ApplyPropertyValue(RichTextBox.ForegroundProperty, newColor);
        });
    }  

_

public CharacterSequenceMappingViewModel SelectedMapping
    {
        get => m_selectedMapping;
        set
        {
            if ( value != null )
            {
                m_selectedMapping = value;
                RaisePropertyChanged(nameof(SelectedMapping));
            }
        }
    }  

0 个答案:

没有答案