3

我想在加载的松散 xaml 文件中定义的 TextBoxes 中设置文本。文本框是应用程序的一部分。

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 ?
            //{
                //

            //}
        }

xaml 文件是这样的:

<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>

这是一个细节视图,必须在不编译新应用程序的情况下进行定制。松散的 xaml 文件的数量及其名称在编译时是未知的。

如何动态设置文本框上的文本?我有一个额外的 xml 文件,可以指定哪些数据来自哪个 TextBox。

4

1 回答 1

0

你能绑定它们吗?如果是这样,我肯定会建议这样做,那么您只需将 设置为DataContext包含这些值的对象。

否则,您可以使用LogicalandVisualTreeHelpers来寻找一个具有正确名称的 TextBox 递归。您也可以尝试FindName一些FrameworkElement包含您的StackPanel.

于 2012-08-03T23:09:26.527 回答