簡體   English   中英

在加載的松散xaml文件中以編程方式設置文本

[英]setting Text programmatically on controls in a loaded loose xaml File

我想在TextBoxes中設置文本,這些文本是在加載的松散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文件及其名稱的數量。

如何動態設置TextBox上的文本? 我有一個額外的xml-File,可以指定哪些數據來自哪個TextBox。

你能他們嗎? 如果是這樣我肯定會建議這樣做,那么你只需要將DataContext設置為包含值的對象。

另外,你可以使用LogicalVisualTreeHelpers ,以遞歸方式查找具有正確名稱的TextBox。 您還可以在包含StackPanel某些FrameworkElement上嘗試FindName

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM