在代码中使用嵌套的 StackPanel 在边框内添加一个文本块

Add a Textblock inside a border with nested StackPanels, in code

我正在尝试从 WPF 代码修改 TextBlock。 我有以下 MainWindow XAML :

<Window x:Class="ChatServer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ChatServer"
    mc:Ignorable="d"
    ResizeMode="NoResize"
    Title="Serveur de chat" Height="450" Width="800">
<Grid x:Name="Grille">
    <DockPanel x:Name="DockP" LastChildFill="False">
        <StackPanel Orientation="Horizontal" DockPanel.Dock="Top" VerticalAlignment="Center" Margin="5,5,0,0" >
            <Border BorderThickness="1" BorderBrush="Blue" DockPanel.Dock="Left" Height="20" Width="380">
                <TextBlock HorizontalAlignment="Center" >Statut du Serveur</TextBlock>
            </Border>
            <Border BorderThickness="1" BorderBrush="Blue" DockPanel.Dock="Right" Height="20" Width="380">
                <TextBlock HorizontalAlignment="Center">Gestion des Clients</TextBlock>
            </Border>
        </StackPanel>
        <StackPanel x:Name="DataZ" Orientation="Horizontal" DockPanel.Dock="Top" VerticalAlignment="Center" Margin="5,0,0,0" >
            <StackPanel x:Name="Server" Orientation="Vertical" DockPanel.Dock="Top" VerticalAlignment="Center" Margin="0,0,0,0">
                <Border BorderThickness="1" BorderBrush="Blue" DockPanel.Dock="Left"  Width="380">
                    <TextBox Name="ServerStatus" HorizontalAlignment="Center" VerticalAlignment="Top" Height="30" Width="380" Text="Serveur éteint" Background="#FFB0B0" TextAlignment="Center"/>
                </Border>
                <Border x:Name="ServerBorder" BorderThickness="1" BorderBrush="Blue" DockPanel.Dock="Left" Height="350" Width="380">
                    <StackPanel x:Name="SP" Orientation="Vertical" DockPanel.Dock="Top" Margin="0,0,0,0">
                        <StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Margin="0,0,0,0">
                        <Button Content="Démarrer" Height="20" Width="60" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,10,10,10" Click="Demarre"/>
                        <Button Content="Arrêter" Height="20" Width="60"  VerticalAlignment="Top" HorizontalAlignment="Right" Margin="10,10,10,10" Click="Arrete"/>
                        </StackPanel>
                        <Border x:Name="LogBorder" BorderThickness="3" BorderBrush="Green" DockPanel.Dock="Left" Height="300" Margin="3,3,3,3">
                        </Border>

                    </StackPanel>
                </Border>

我想在“LogBorder”边框(最后一个)内添加一个文本块。这是代码(不起作用):

public MainWindow()
{
    InitializeComponent();
    void CreateServerLogText()
    {
        TextBlock ServerLog = new TextBlock();
        // <Border x:Name="LogBorder" BorderThickness="3" BorderBrush="Green" DockPanel.Dock="Left" Height="300" Margin="3,3,3,3">
        //      <TextBlock x:Name="ServerLog" Text="Logs du serveur" TextWrapping="Wrap"/>
        // </ Border >
        Grille.LogBorder.Child = ServerLog; // NOT WORKING
        // Grille.DockP.DataZ.Server.ServerBorder.SP.LogBorder.Children.Add(ServerLog); // NOT WORKING
    }
    CreateServerLogText();
}

如何在 XAML 树中“导航”以添加或修改 StackPanel 和 Border 中的内容?

有点像

        TextBlock CreateServerLogText()
        {
            TextBlock ServerLog = new TextBlock();
            LogBorder.Child = ServerLog;
            return ServerLog;
        }
        TextBlock ServerLog = CreateServerLogText();

LogBorder 字段是 MainWindow 的直接成员。即使边框嵌套在 xaml 中的另一个控件内,字段本身也没有嵌套。只需删除 Grille. 即可使其正常工作。您必须区分声明字段的静态 C# 代码和 WPF 在 运行 时间根据 xaml 代码动态创建的嵌套数据结构。

这个测试代码对我有用:

public MainWindow()
{
    InitializeComponent();
    TextBlock ServerLog = new TextBlock { Text = "Hello world" };
    LogBorder.Child = ServerLog;
}

旁注:“不工作”不足以描述问题。 “工作”或“不工作”需要代码编译并 运行。只有当它 运行s 时,您才能测试它是否按预期工作。

在你的例子中,代码没有编译。你得到编译器错误

Error CS1061 'Grid' does not contain a definition for 'LogBorder' and no accessible extension method 'LogBorder' accepting a first argument of type 'Grid' could be found (are you missing a using directive or an assembly reference?)

该错误告诉您网格 (Grille) 不包含预期的 LogBorder 字段。这是正确的,因为它包含在 MainWindow.