2012-08-03 13 views
3

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

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文件,可以指定哪些数据来自哪个文本框。

回答

0

你能bind他们吗?如果是这样我肯定会推荐这么做,那么你只需要将DataContext设置为包含这些值的对象。

否则,您可以用LogicalVisualTreeHelpers猴子周围,递归地寻找具有正确名称的文本框。您也可以尝试FindName,其中包含您的StackPanel

+0

谢谢DataBinding实际上工作:) – user1550097 2012-08-03 23:55:36

+0

@ user1550097:很高兴帮助:) – 2012-08-03 23:56:58

相关问题