locked
Why does my UserControl not show its content? RRS feed

  • Question

  • I have an application that I wrote long ago in SL 3.x. I have converted it all over to SL 5. Everything works EXCEPT one part. I have a WrapPanel whose children I add dynamically at runtime. I can see in the debugger that everything appears to be okay, no null objects, no complaints in the output window, but I see no content of my WrapPanel child. I can tell that the child was added because the control grows, but nothing shows.

    Here is my UserControl:

    <UserControl 
       
       x:Class="EDBSilverlight.CommonParts.LegendItem"
       
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Background="White"
       >
       <Grid x:Name="LayoutRoot" Margin="4,2,4,2" >
          <TextBlock Text="test" Foreground="Black" FontSize="24" />
       </Grid>
    </UserControl>


    Here is my code to create instances at runtime:

          procedure CreateLegend;
             begin
             var legendItem            := new LegendItem;
             SaveRatmChart.MyLegendPanel.Children.Add( legendItem );
             end;

    The LegendItem never shows.  I added a button to create new ones and I can see that the MyLegendPanel which is just a WrapPanel is growing in size and seems to show the content I add.  It is like the LegendItem doesn't paint itself for some reason when it first displays that is beyond me.

    Here is that LegendPanel:

    <my3:WrapPanel  

    x:Name="MyLegendPanel"   Grid.Row="1"   HorizontalAlignment="Center"   VerticalAlignment="Bottom"   Margin="50,0,50,0"   />


    This code worked in SL 3.x.






    • Edited by Mark Tiede Friday, February 19, 2016 6:19 PM
    Friday, February 19, 2016 4:40 PM

All replies

  • They appear down the bottom of my window,  but this works fine for me:

    <UserControl
        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:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" x:Class="SilverlightApplication1.MainPage"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
    
        <Grid x:Name="LayoutRoot" Background="White">
            <toolkit:WrapPanel 
                Name="wp"
                HorizontalAlignment="Center"   VerticalAlignment="Bottom" 
                />
        </Grid>
    </UserControl>

    uc

    <UserControl x:Class="SilverlightApplication1.UC1"
        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"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
        
            <Grid x:Name="LayoutRoot" Margin="4,2,4,2" >
                <TextBlock Text="test" Foreground="Black" FontSize="24" />
            </Grid>
    </UserControl>

    code behind in mainpage

        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                for (int i = 0; i < 10; i++)
                {
                    wp.Children.Add(new UC1());
                }
            }
        }

    looks like


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Saturday, February 20, 2016 10:43 AM
    Moderator
  • Not sure what the problem with my code was. I could make instances of the UC in a button press when everything was loaded too. But DURING the load process, there was some problem.

    I ended up scrapping doing anything with the Children and instead just made a List which was bound to the MyLegendPanel's ItemsSource.  And the panel's content was an ItemsControl with an ItemPanel Template and an ItemTemplate.  Although all the code was being done in the same places as before, using a simple binding worked fine. 

    I imagine that back when I wrote the original code, I didn't know how to use an ItemsControl.

    So I worked around whatever the problem was.



    • Edited by Mark Tiede Monday, February 29, 2016 1:16 PM
    Monday, February 29, 2016 1:11 PM