Question

I want to set text in TextBoxes that are defined in an loaded loose xaml file. The TextBoxes are part of the application.

StackPanel LooseRoot;

        if (fTeleFound == true)
        {
            try
            {
                using (System.IO.FileStream fs = new System.IO.FileStream(sXamlFileName, System.IO.FileMode.Open))
                {
                    LooseRoot = (StackPanel)System.Windows.Markup.XamlReader.Load(fs);
                }
                this.DetailsViewFrame.Children.Add(LooseRoot);
            }
            catch (ArgumentException ex)
            {
                // TBD Error xamlfile
                return;
            }


            // set Elements
            //foreach (TextBox textBox in LooseRoot.Children as TextBox) // does not work ?
            //{
                //

            //}
        }

The xaml file is like this:

<StackPanel Margin="10" Orientation="Vertical"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

<StackPanel Orientation="Horizontal">
    <Label Margin="5,5,0,5">CRCDB=</Label>
    <TextBox Margin="0,5,5,5" Name="DetailsviewTB1" Height="20"/>

    <Label Margin="10,5,0,5">Infonummer=</Label>
    <TextBox Margin="0,5,5,5" Name="DetailsviewTB2" Height="20"/>

    <Label Margin="10,5,0,5">Daten=</Label>
    <TextBox Margin="0,5,5,5" Name="DetailsviewTB3" Height="20"/>
</StackPanel>

This is for a details view that has to be customizable without compiling the application new. The amount of loose xaml files and their names are not known at compiling time.

How can i set the Text on the TextBoxes dynamicly? I have a additional xml-File that can specify what data comes in which TextBox.

Was it helpful?

Solution

Can you bind them? If so i would definitely recommend doing that, then you just need to set the DataContext to an object containing the values.

Else you can monkey around with Logical and VisualTreeHelpers, looking for a TextBox with the right name recursively. You can also try FindName on some FrameworkElement containing your StackPanel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top