【问题标题】:Converting Inline back to WatermarkTextBox将 Inline 转换回 WatermarkTextBox
【发布时间】:2014-07-21 11:32:45
【问题描述】:

我将 WPF 工具包用于其WatermarkTextBox 控件。

我的应用程序动态连接文本字符串和占位符以供用户填写空白。

foreach(var e in elements)
{
    if (isText)
    {
        LetterText.Inlines.Add(new Run
        {
            Text = e,
            BaselineAlignment = BaselineAlignment.Center
        });
    }
    else
    {
        LetterText.Inlines.Add(new WatermarkTextBox
        {
            Watermark = e
        });
    }

    isText = !isText;
}

这很好用,但是当我想重新组合文本时出现问题:

foreach(var inline in LetterText.Inlines)
{
    if (inline.GetType() == typeof(Run))
    {
        sb.Append(((Run)inline).Text);
    }
    else if (inline.GetType() == typeof(WatermarkTextBox))
    {
        var wtb = inline as WatermarkTextBox;
        sb.Append(wtb.Text);
    }
}

这在编译时失败,出现“无法将 Inline 转换为 WatermarkTextBox”(没有 else 子句,从 InlineRun 的转换工作正常)。

如何从WatermarkTextBox 获取文本?

【问题讨论】:

    标签: wpf wpftoolkit


    【解决方案1】:

    这成功了:

                if (inline.GetType() == typeof(Run))
                {
                    sb.Append(((Run)inline).Text);
                }
                else if (inline.GetType() == typeof(InlineUIContainer))
                {
                    var container = inline as InlineUIContainer;
                    var wtb = container.Child as WatermarkTextBox;
    
                    if (wtb !=null)
                        sb.Append(wtb.Text);
                }
    

    我不知道为什么 InlineUIContainer 也不会为 Run 元素隐式创建

    【讨论】:

      猜你喜欢
      • 2020-09-09
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      相关资源
      最近更新 更多