在已加载的松散xaml文件中的控件上以编程方式设置Text
本文关键字:编程 方式 设置 控件 Text 文件 加载 xaml | 更新日期: 2024-10-22 07:01:59
我想在加载的松散xaml文件中定义的TextBoxes中设置文本。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中包含哪些数据。
你能绑定它们吗?如果是这样的话,我肯定建议您这样做,那么您只需要将DataContext
设置为包含值的对象。
否则,您可以随意使用Logical
和VisualTreeHelpers
,递归地查找具有正确名称的TextBox。您也可以在包含StackPanel
的某些FrameworkElement
上尝试FindName
。