[英]Displaying Border on TextBlock when TextBlock placed inside ScrollViewer
[英]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 中的内容?
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 指令或程序集引用?)
该错误告诉您 Grid (Grille) 不包含预期的LogBorder
字段。 这是正确的,因为它包含在MainWindow
。
它就像
TextBlock CreateServerLogText()
{
TextBlock ServerLog = new TextBlock();
LogBorder.Child = ServerLog;
return ServerLog;
}
TextBlock ServerLog = CreateServerLogText();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.