【问题标题】:Add a Textblock inside a border with nested StackPanels, in code在代码中使用嵌套的 StackPanel 在边框内添加一个文本块
【发布时间】:2021-09-29 18:27:20
【问题描述】:

我正在尝试从 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 中添加或修改内容?

【问题讨论】:

    标签: c# wpf dockpanel


    【解决方案1】:

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

    这个测试代码对我有用:

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

    旁注:“不工作”并不是对问题的充分描述。 “工作”或“不工作”需要代码编译和运行。只有当它运行时,你才能测试它是否按预期工作。

    在您的情况下,代码未编译。你得到编译器错误

    错误 CS1061“Grid”不包含“LogBorder”的定义,并且找不到接受“Grid”类型的第一个参数的可访问扩展方法“LogBorder”(您是否缺少 using 指令或程序集引用?)

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

    【讨论】:

      【解决方案2】:

      有点像

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-30
        • 1970-01-01
        • 2019-03-10
        • 2012-08-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多