TheSHEEEP
Topic Author
Posts: 37
Joined: 13 Sep 2012, 10:52

Element added to DockPanel at runtime not shown

24 Oct 2013, 14:36

Hey,

I am using the following code to add a TextBlock at runtime to an existing DockPanel (which came from a xaml file):
// 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);
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:
<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>
Maybe I'm doing something simple somehow very wrong :D
Last edited by TheSHEEEP on 25 Oct 2013, 14:34, edited 1 time in total.
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: Element added to DockPanel at runtime not shown

24 Oct 2013, 17:31

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.
 
TheSHEEEP
Topic Author
Posts: 37
Joined: 13 Sep 2012, 10:52

Re: Element added to DockPanel at runtime not shown

24 Oct 2013, 18:40

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:
<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>
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: Element added to DockPanel at runtime not shown

25 Oct 2013, 01:19

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:
<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>
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):
<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>
 
TheSHEEEP
Topic Author
Posts: 37
Joined: 13 Sep 2012, 10:52

Re: Element added to DockPanel at runtime not shown

25 Oct 2013, 14:34

Ah, yes. Now it works, thanks!

What I had to do instead of calling Add() on the DockPanel children, was calling Insert:
((Noesis::Gui::DockPanel*)status->GetParent())->GetChildren()->Insert(0, _fpsText);
Noesis::Gui::DockPanel::SetDock(_fpsText, Noesis::Gui::Dock_Top);
Now it is at the top, where I want it to be :)

Who is online

Users browsing this forum: No registered users and 5 guests