views:

20

answers:

1

Hello.

Is there a way to modify a UIElement's contents?

I have something like this:

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

And when I run the debugger and add uie to the "Watch" window, it gives me the following:

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

Now I need to be able to do two things: (1) read the Text in the textbox, and (2) modify it whenever I want.

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

, but it does not work like that. If anyone can point me to the method that performs this function I'd really appreciate it.

+1  A: 

TextBox is a UIElement. When you load the XAML out of the reader you could cast it to a TextBox at that point in time; however since you are casting it to a UIElement you could at whatever point in time go...

if (uie is TextBox)
{
   TextBox tb = (TextBox)uie;
   b.text = "New title";
}
Aaron
@Aaron: Thank you very much; It works!