Element added to DockPanel at runtime not shown
Hey,
I am using the following code to add a TextBlock at runtime to an existing DockPanel (which came from a xaml file):
But the added text is not there. Nothing is added to the panel,it seems.
I can hide/manipulate existing elements inside the panel at runtime without a problem, but this TextBlock is never visible. It doesn't crash, so I suppose it is there somehow, but not being rendered.
Here is the original DockPanel xaml:
Maybe I'm doing something simple somehow very wrong 
I am using the following code to add a TextBlock at runtime to an existing DockPanel (which came from a xaml file):
Code: Select all
// Insert our fps text field
_fpsText = new Noesis::Gui::TextBlock();
_fpsText->SetText("FPS: 0");
_fpsText->SetFontSize(20.0f);
_fpsText->SetTextAlignment(Noesis::Gui::TextAlignment_Center);
_fpsText->SetForeground((Noesis::Gui::Brush*)Noesis::Gui::Brushes::White());
// Ugly workaround, but the DockPanel does not have a name
// TODO: fix when on Windows
Noesis::Gui::TextBlock* status = (Noesis::Gui::TextBlock*)_xamlRoot->FindName("txtStatus");
((Noesis::Gui::DockPanel*)status->GetParent())->GetChildren()->Add(_fpsText);
Noesis::Gui::DockPanel::SetDock(_fpsText, Noesis::Gui::Dock_Bottom);
I can hide/manipulate existing elements inside the panel at runtime without a problem, but this TextBlock is never visible. It doesn't crash, so I suppose it is there somehow, but not being rendered.
Here is the original DockPanel xaml:
Code: Select all
<DockPanel>
<TextBlock x:Name="txtStatus"
DockPanel.Dock="Bottom" Margin="0,0,0,10" FontSize="20" Foreground="White" Text="I am OK!" TextAlignment="Center"/>
<TextBlock
DockPanel.Dock="Top" Margin="0,0,0,10" FontSize="24" Foreground="White" Text="The Player" TextAlignment="Center"/>
<Image x:Name="imgNorm"
DockPanel.Dock="Top" Source="images/jan_ok.png" Stretch="Uniform"/>
<Image x:Name="imgJump"
DockPanel.Dock="Top" Visibility="Collapsed" Source="images/jan_jump.png" Stretch="Uniform"/>
</DockPanel>

Last edited by TheSHEEEP on 25 Oct 2013, 14:34, edited 1 time in total.
-
-
sfernandez
Site Admin
- Posts: 3203
- Joined:
Re: Element added to DockPanel at runtime not shown
Hi,
I just tried the same code and it worked perfectly.
Is it possible that the window background is white and _fpsText, that it is also white, is not visible because of that?
If you are not able to make it work, please create a bug report in our bugtracker attaching the xaml and code that is failing, and we will try to figure out what is happening.
I just tried the same code and it worked perfectly.
Is it possible that the window background is white and _fpsText, that it is also white, is not visible because of that?
If you are not able to make it work, please create a bug report in our bugtracker attaching the xaml and code that is failing, and we will try to figure out what is happening.
Re: Element added to DockPanel at runtime not shown
I am using a slightly older version on Linux (it was before the official Linux release, I think version 1.01 or so?).
Was something related to this changed recently, maybe?
This may be a bug already fixed, after all. But I currently do not have the time to update to the very latest version.
The background of that DockPanel is definitely not white
Here is the full xaml of our UI, maybe you can reproduce the bug with that:
Was something related to this changed recently, maybe?
This may be a bug already fixed, after all. But I currently do not have the time to update to the very latest version.
The background of that DockPanel is definitely not white

Here is the full xaml of our UI, maybe you can reproduce the bug with that:
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<!-- Define rows & columns -->
<Grid.RowDefinitions>
<RowDefinition Height="60*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="30*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20*"/>
<ColumnDefinition Width="80*"/>
</Grid.ColumnDefinitions>
<!-- Define resources -->
<Grid.Resources>
<LinearGradientBrush x:Key="BgBrushRed" StartPoint="0.2,0" EndPoint="0.8,1">
<GradientStop Offset="0" Color="#BB962929"/>
<GradientStop Offset="1" Color="#BBE80C0C"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="BgBrushBlue" StartPoint="0.2,0" EndPoint="0.8,1">
<GradientStop Offset="0" Color="#BBFF0000"/>
<GradientStop Offset="1" Color="#BB0000FF"/>
</LinearGradientBrush>
<Storyboard x:Key="Anim" Duration="0:0:2" RepeatBehavior="Forever"
TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" TargetName="btnLuaTest">
<DoubleAnimation From="0" To="-360" Duration="0:0:2" RepeatBehavior="Forever"/>
</Storyboard>
</Grid.Resources>
<!-- Define triggers -->
<Grid.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Name="ButtonAnim" Storyboard="{StaticResource Anim}"/>
</EventTrigger>
</Grid.Triggers>
<!-- Player status -->
<Border Background="{DynamicResource BgBrushRed}" BorderBrush="Black" BorderThickness="2" CornerRadius="8"
Margin="0,20,0,0" Grid.Row="2" Grid.Column="0">
<DockPanel>
<TextBlock x:Name="txtStatus"
DockPanel.Dock="Bottom" Margin="0,0,0,10" FontSize="20" Foreground="White" Text="I am OK!" TextAlignment="Center"/>
<TextBlock
DockPanel.Dock="Top" Margin="0,0,0,10" FontSize="24" Foreground="White" Text="The Player" TextAlignment="Center"/>
<Image x:Name="imgNorm"
DockPanel.Dock="Top" Source="images/jan_ok.png" Stretch="Uniform"/>
<Image x:Name="imgJump"
DockPanel.Dock="Top" Visibility="Collapsed" Source="images/jan_jump.png" Stretch="Uniform"/>
</DockPanel>
</Border>
<!-- Button test -->
<Border x:Name="border" Background="{DynamicResource BgBrushBlue}" BorderBrush="Black" BorderThickness="2" CornerRadius="8"
Margin="0,20,0,0" Grid.Row="1" Grid.Column="0">
<Grid VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="20*"/>
<RowDefinition Height="60*"/>
<RowDefinition Height="20*"/>
</Grid.RowDefinitions>
<Viewbox Stretch="Uniform" Grid.Row="1">
<Button x:Name="btnLuaTest" RenderTransformOrigin="0.5, 0.5"
Content="Call Lua script" Margin="3" FontSize="16" HorizontalAlignment="Stretch">
<Button.RenderTransform>
<RotateTransform Angle="0" CenterX="0.5" CenterY="0.5"/>
</Button.RenderTransform>
</Button>
</Viewbox>
</Grid>
</Border>
<!-- Transparent backgrounds -->
<DockPanel Background="Transparent" Grid.Row="2" Grid.Column="0"/>
<DockPanel Background="Transparent" Grid.Row="0" Grid.Column="1"/>
<DockPanel Background="Transparent" Grid.Row="1" Grid.Column="1"/>
<DockPanel Background="Transparent" Grid.Row="2" Grid.Column="1"/>
</Grid>
-
-
sfernandez
Site Admin
- Posts: 3203
- Joined:
Re: Element added to DockPanel at runtime not shown
I finally understood what was happening with your design
The reason why your fps text was not rendered is because the Image that is measured before is taking all the remaining space in the DockPanel (Image has no Width or Height and has the Stretch setted as Uniform, so it stretches to all available space).
If you try the following xaml in Blend or Kaxaml you will check that the last text is not rendered:
To ensure that the fps text can get some space in the measure pass, it should be inserted before the image (or path in my example):

The reason why your fps text was not rendered is because the Image that is measured before is taking all the remaining space in the DockPanel (Image has no Width or Height and has the Stretch setted as Uniform, so it stretches to all available space).
If you try the following xaml in Blend or Kaxaml you will check that the last text is not rendered:
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="Black">
<DockPanel>
<TextBlock x:Name="txtStatus" DockPanel.Dock="Bottom" Margin="0,0,0,10" FontSize="20" Foreground="White" Text="I am OK!" TextAlignment="Center"/>
<TextBlock DockPanel.Dock="Top" Margin="0,0,0,10" FontSize="24" Foreground="White" Text="The Player" TextAlignment="Center"/>
<Path DockPanel.Dock="Top" Data="M0,0 L100,0 50,50 Z" Fill="Red" Stretch="Uniform"/>
<TextBlock DockPanel.Dock="Bottom" Margin="0,0,0,10" FontSize="20" Foreground="White" Text="FPS: 0" TextAlignment="Center"/>
</DockPanel>
</Grid>
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="Black">
<DockPanel>
<TextBlock x:Name="txtStatus" DockPanel.Dock="Bottom" Margin="0,0,0,10" FontSize="20" Foreground="White" Text="I am OK!" TextAlignment="Center"/>
<TextBlock DockPanel.Dock="Top" Margin="0,0,0,10" FontSize="24" Foreground="White" Text="The Player" TextAlignment="Center"/>
<TextBlock DockPanel.Dock="Bottom" Margin="0,0,0,10" FontSize="20" Foreground="White" Text="FPS: 0" TextAlignment="Center"/>
<Path DockPanel.Dock="Top" Data="M0,0 L100,0 50,50 Z" Fill="Red" Stretch="Uniform"/>
</DockPanel>
</Grid>
Re: Element added to DockPanel at runtime not shown
Ah, yes. Now it works, thanks!
What I had to do instead of calling Add() on the DockPanel children, was calling Insert:
Now it is at the top, where I want it to be 
What I had to do instead of calling Add() on the DockPanel children, was calling Insert:
Code: Select all
((Noesis::Gui::DockPanel*)status->GetParent())->GetChildren()->Insert(0, _fpsText);
Noesis::Gui::DockPanel::SetDock(_fpsText, Noesis::Gui::Dock_Top);

Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 8 guests