手动修改或清除文本时,RichTextBox绑定断开 [英] RichTextBox Binding is broken when text is manually modified or cleared
问题描述
我有一个绑定到字符串的RichTextBox
.
I have a RichTextBox
bound to a string.
使用C#生成一个写入其中的字符串.
Using C# I generate a string that writes to it.
但是,如果我想通过单击RichTextBox
并使用Backspace键将其删除,或者按Enter来换行来手动更改文本,则绑定会断开,并且我无法再用字符串第二次.
But if I want to manually change the text by clicking into the RichTextBox
and deleting it with the backspace key, or pressing Enter to make a new line, the binding becomes broken and I can no longer programmatically write to it with the string a second time.
XAML
<RichTextBox x:Name="rtbScriptView"
Margin="11,71,280,56"
Padding="10,10,10,48"
FontSize="14"
Grid.ColumnSpan="1"
VerticalScrollBarVisibility="Auto"
RenderOptions.ClearTypeHint="Enabled"
Style="{DynamicResource RichTextBoxStyle}">
<FlowDocument>
<Paragraph>
<Run Text="{Binding ScriptView_Text,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" />
</Paragraph>
</FlowDocument>
</RichTextBox>
查看模型
private string _ScriptView_Text;
public string ScriptView_Text
{
get { return _ScriptView_Text; }
set
{
if (_ScriptView_Text == value)
{
return;
}
_ScriptView_Text = value;
OnPropertyChanged("ScriptView_Text");
}
}
C#
ViewModel vm = new ViewModel();
DataContext = vm;
// Display a string in the RichTextBox
vm.ScriptView_Text = "This is a test."; // <-- This won't work if text is manually modified
推荐答案
编辑RichTextBox时,将更改FlowDocument元素内部的元素.绑定的元素可能会在此编辑过程中的某个时候被删除. 看看RichtTextBox.Document.Groups,看看编辑RichTextBox时发生了什么.
When you edit the RichTextBox, you alter the elements inside of the FlowDocument element. The element you have a binding on, is probably removed at some point during this editing. Have a look at RichtTextBox.Document.Groups to see what's happening when you edit the RichTextBox.
默认的RichTextBox不能很好地支持MVVM/Binding.您希望在Document属性上具有一个绑定,但是默认的RichTextBox不支持此绑定. 您可以此处.
The default RichTextBox does not really support MVVM/Binding very well. You'd want to have a binding on the Document property, but this is not supported for the default RichTextBox. You could have a look here.
还是自己扩展它,像这样?
Or extend it yourself, something like this?:
BindableRichTextBox类
public class BindableRichTextBox : RichTextBox
{
public static readonly DependencyProperty DocumentProperty = DependencyProperty.Register(nameof(Document), typeof(FlowDocument), typeof(BindableRichTextBox), new FrameworkPropertyMetadata(null, OnDocumentChanged));
public new FlowDocument Document
{
get => (FlowDocument)GetValue(DocumentProperty);
set => SetValue(DocumentProperty, value);
}
public static void OnDocumentChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
var rtb = (RichTextBox)obj;
rtb.Document = args.NewValue != null ? (FlowDocument)args.NewValue : new FlowDocument();
}
}
XAML
<controls:BindableRichTextBox Document="{Binding YourFlowDocumentObject, Mode=OneWay}"/>
然后您可以从FlowDocument中获取字符串.
Then you can get the string from the FlowDocument.
这篇关于手动修改或清除文本时,RichTextBox绑定断开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!