【问题标题】:How can I modify a UIElement's contents?如何修改 UIElement 的内容?
【发布时间】:2010-10-25 20:28:37
【问题描述】:

有没有办法修改 UIElement 的内容?

我有这样的事情:

System.IO.FileStream rdr = System.IO.File.OpenRead(xamlFilePath);
System.Windows.UIElement uie = (System.Windows.UIElement)System.Windows.Markup.XamlReader.Load(rdr);

当我运行调试器并将 uie 添加到“Watch”窗口时,它给了我以下信息:

[-]uie
[-]System.Windows.Controls.Textbox          {System.Windows.Controls.Textbox:Title}
<some stuff...>
Text                                        "Title"

现在我需要能够做两件事:(1) 阅读文本框中的文本,以及 (2) 随时修改。

I was hoping for something in the lines of:
{
Textbox tb = (Textbox)uie.GetChild();
tb.Text = "New Title"
uie.SetChild(tb);
}

,但它不是那样工作的。如果有人能指出执行此功能的方法,我将不胜感激。

【问题讨论】:

  • 请提供您的 XAML 文件的内容。

标签: wpf xaml textbox children


【解决方案1】:

TextBoxUIElement。当您从阅读器中加载 XAML 时,您可以在该时间点将其转换为 TextBox;但是,由于您将其转换为UIElement,因此您可以在任何时间点去...

if (uie is TextBox)
{
   TextBox tb = (TextBox)uie;
   b.text = "New title";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 2014-07-20
    • 2011-03-19
    相关资源
    最近更新 更多