在已加载的松散 XAML 文件中,以编程方式设置控件的文本。

3

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

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

            //}
        }
文件如下所示:
<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文件,可以指定哪些数据出现在哪个文本框中。

1个回答

0

你能够绑定它们吗?如果可以,我强烈推荐这样做,然后你只需要将DataContext设置为一个包含值的对象。

否则,你可以尝试使用LogicalVisualTreeHelpers来查找具有适当名称的TextBox。你也可以在包含你的StackPanel的一些FrameworkElement上尝试使用FindName.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接