I am trying to modify a TextBlock from a WPF code. I have the following 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>
I would like to add a Textblock inside the "LogBorder" Border (the last one). Here is the code (not working) :
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();
}
How do you "navigate" inside the XAML tree to add or modify content inside StackPanel and Border ?
The LogBorder
field is a direct member of MainWindow
. Even if the border is nested inside another control in xaml, the field itself is not nested. Simply remove Grille.
to make it work. You must differentiate between the static C# code where the field is declared, and the nested data structure created dynamically at runtime by WPF based on the xaml code.
This test code works for me:
public MainWindow()
{
InitializeComponent();
TextBlock ServerLog = new TextBlock { Text = "Hello world" };
LogBorder.Child = ServerLog;
}
Side note: "not working" is not an adequate description of the problem. "Working" or "not working" requires the code to compile and to run. Only when it runs, you can test whether it is working as expected or not.
In your case, the code is not compiling. You get the compiler error
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?)
The error tells you that the Grid (Grille) does not contain the expected LogBorder
field. Which is correct since it is contained in MainWindow
.