gpt4 book ai didi

c# - WinRT : Binding a RTF String to a RichEditBox

转载 作者:行者123 更新时间:2023-11-30 19:16:11 26 4
gpt4 key购买 nike

搜索了很长时间以将一些 RTF 文本绑定(bind)到 Windows 应用商店应用程序上的 RichEditBox 控件。即使它应该在 TwoMay 绑定(bind)模式下运行。...

最佳答案

...最后我找到了以下解决方案。我使用 DependencyProperty RtfText 从原始 RichEditBox 控件创建了一个继承控件。

public class RichEditBoxExtended : RichEditBox
{
public static readonly DependencyProperty RtfTextProperty =
DependencyProperty.Register(
"RtfText", typeof (string), typeof (RichEditBoxExtended),
new PropertyMetadata(default(string), RtfTextPropertyChanged));

private bool _lockChangeExecution;

public RichEditBoxExtended()
{
TextChanged += RichEditBoxExtended_TextChanged;
}

public string RtfText
{
get { return (string) GetValue(RtfTextProperty); }
set { SetValue(RtfTextProperty, value); }
}

private void RichEditBoxExtended_TextChanged(object sender, RoutedEventArgs e)
{
if (!_lockChangeExecution)
{
_lockChangeExecution = true;
string text;
Document.GetText(TextGetOptions.None, out text);
if (string.IsNullOrWhiteSpace(text))
{
RtfText = "";
}
else
{
Document.GetText(TextGetOptions.FormatRtf, out text);
RtfText = text;
}
_lockChangeExecution = false;
}
}

private static void RtfTextPropertyChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var rtb = dependencyObject as RichEditBoxExtended;
if (rtb == null) return;
if (!rtb._lockChangeExecution)
{
rtb._lockChangeExecution = true;
rtb.Document.SetText(TextSetOptions.FormatRtf, rtb.RtfText);
rtb._lockChangeExecution = false;
}
}
}

这个解决方案对我有用——也许对其他人也有用。 :-)

已知问题:VirtualizingStackPanel.VirtualizationMode="Recycling"

中的奇怪行为

关于c# - WinRT : Binding a RTF String to a RichEditBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26549156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com