Silverlight4 how to make Canvas ZIndex top-most when inside a StackPanel

Problem: Need to fly a message above all other elements on screen, where the obvious solution of using a Canvas messes up the use of a list-wrapping display.

Background: I have a Silverlight4 application that is working great, correctly displaying a ListBox with a WrapPanel item template where it must be configured to use up all remaining space on the screen, after allowing for a heading area on top.  All of this is coordinated through a StackPanel to lay out the overall screen.

I need to be able to display messages on top of all of this without disturbing the layout. A Canvas tucked inside the heading area is working great, and I can position messages with the canvas exactly where I want.

The only problem is the message is always behind the other items on screen regardless of the ZIndex. This makes sense from the perspective that the other items are not inside the canvas but rather the canvas is inside the other items, rendering the ZIndex moot.

When I try to enclose the entire screen layout in a Canvas it looses desired WrapPanel behavior, which is essential to the design.

Question: Is there a way to fly an element above all others without basing the entire layout on a Canvas?  My only idea is a ChildWindow without a border but that seems overkill for what I need to do.

LVL 11
Craig YellickDatabase ArchitectAsked:
Who is Participating?
 
vbighamConnect With a Mentor Commented:
I have been able to use the Canvas.ZIndex property without ever using a canvas on the page.  

What I have done in the past is add the message control to the root grid, or the desired container.  Then set the Grid.ColumnSpan and Grid.RowSpan(if necessary), and either set the ZIndex, or put it at the very bottom of the XAML (the most recent additions to a grids children seem to show up on top).  Then you  should be able to just switch the visibility.  

Something like the following.
<navigation:Page x:Class="SilverlightSampleNavigation.About" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                 xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" 
           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"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           d:DesignWidth="640" d:DesignHeight="480"
           Title="About" 
           Style="{StaticResource PageStyle}">

  <Grid x:Name="LayoutRoot">
        <UserControl>
            <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="70"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <StackPanel Grid.Row="0" Grid.Column="0" Orientation="Horizontal">
                    <HyperlinkButton HorizontalAlignment="Left" Content="Link1" FontSize="14" Margin="2,0" />
                    <HyperlinkButton HorizontalAlignment="Left" Content="Link2" FontSize="14" Margin="2,0" />
                    <HyperlinkButton HorizontalAlignment="Left" Content="Link3" FontSize="14" Margin="2,0" />
                </StackPanel>

                <StackPanel Grid.Row="1" Grid.Column="0" Orientation="Vertical" >                    
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="SUB HEADING" Margin="5,0,0,0" FontSize="32" FontStyle="Italic" FontWeight="ExtraBold" VerticalAlignment="Bottom" />
                        <StackPanel Orientation="Vertical" Margin="10,0,0,0">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="More Information" FontSize="14" VerticalAlignment="Center" />
                                <TextBlock  Text="(Message)" Margin="6,0,0,0" VerticalAlignment="Center" FontSize="14" FontWeight="Bold"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Sort by:" FontSize="14" VerticalAlignment="Center" />
                                <RadioButton Content="Aaaa" FontSize="14" Margin="10,0,0,0" VerticalAlignment="Center" />
                                <RadioButton Content="Bbbb" FontSize="14" Margin="10,0,0,0" VerticalAlignment="Center" />
                            </StackPanel>
                        </StackPanel>
                    </StackPanel>
                </StackPanel>

                <StackPanel Grid.Row="0" Grid.Column="1" Grid.RowSpan="2"  Orientation="Vertical" HorizontalAlignment="Right">
                    <TextBlock Text="MAIN HEADING" FontWeight="Bold" FontSize="38" Padding="0,0,15,0" />
                    <TextBlock Text="Additional Information" FontSize="18" />
                </StackPanel>

                <ListBox x:Name="ListBox1" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <toolkit:WrapPanel Orientation="Vertical" />
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.Template>
                        <ControlTemplate>
                            <Grid>
                                <ItemsPresenter />
                            </Grid>
                        </ControlTemplate>
                    </ListBox.Template>
                </ListBox>

                <Grid Grid.ColumnSpan="999" Grid.RowSpan="999" x:Name="ErrorMessageGrid" Background="#86000000">
                    <Border  x:Name="ErrorMessageBorder" Width="500" Canvas.Top="50" Canvas.Left="150"  
                            Background="Red" BorderThickness="5" CornerRadius="10" Height="200">
                        <TextBlock x:Name="ErrorMessageTextBlock"  
                     Text="This is an Error Message and should be displayed on top of all other content."     
                     Padding="6" Margin="6"  
                     FontSize="18" FontWeight="Bold" FontStyle="Italic" TextWrapping="Wrap">
                        </TextBlock>
                    </Border>
                </Grid>
            </Grid>
        </UserControl>

    </Grid>

</navigation:Page>

Open in new window

0
 
BinuthCommented:
can you post sample code/app ?
0
 
Craig YellickDatabase ArchitectAuthor Commented:
This first snippet shows how using a Canvas as the root layout element allows me to display the error message on top of all other content, which is the goal. However, note that the WrapPanel no longer functions and the ListBox of 24 items runs off-screen instead of wrapping.  The 24 items are added via the code below.

  Public Sub New()
    InitializeComponent()
    For i As Integer = 1 To 24
      Dim item As New ListBoxItem()
      item.Content = String.Format("Item {0:00}.", i)
      item.FontSize = 36
      ListBox1.Items.Add(item)
    Next
  End Sub

 
<UserControl x:Class="CanvasProblem.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"  
  >
  <Canvas>
    <Border x:Name="ErrorMessageBorder" Width="500" Canvas.Top="50" Canvas.Left="150" Canvas.ZIndex="999"
                            Background="Red" BorderThickness="5" CornerRadius="10">
      <TextBlock x:Name="ErrorMessageTextBlock"
                     Text="This is an Error Message and should be displayed on top of all other content."  
                     Padding="6" Margin="6"
                     FontSize="18" FontWeight="Bold" FontStyle="Italic" TextWrapping="Wrap">
      </TextBlock>
    </Border>
  
    <Grid Canvas.Top="0" Canvas.Left="0"  >
      <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="70"/>
        <RowDefinition Height="*"/>
      </Grid.RowDefinitions>

      <StackPanel Grid.Row="0" Grid.Column="0" Orientation="Horizontal">
        <HyperlinkButton Content="Link1" FontSize="14" Margin="2,0" />
        <HyperlinkButton Content="Link2" FontSize="14" Margin="2,0" />
        <HyperlinkButton Content="Link3" FontSize="14" Margin="2,0" />
       </StackPanel>

      <StackPanel Grid.Row="1" Grid.Column="0" Orientation="Vertical" >
        <StackPanel Orientation="Horizontal">
          <TextBlock Text="SUB HEADING" Margin="5,0,0,0" FontSize="32" FontStyle="Italic" FontWeight="ExtraBold" VerticalAlignment="Bottom" />
          <StackPanel Orientation="Vertical" Margin="10,0,0,0">
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="More Information" FontSize="14" VerticalAlignment="Center" />
              <TextBlock  Text="(Message)" Margin="6,0,0,0" VerticalAlignment="Center" FontSize="14" FontWeight="Bold"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="Sort by:" FontSize="14" VerticalAlignment="Center" />
              <RadioButton Content="Aaaa" FontSize="14" Margin="10,0,0,0" VerticalAlignment="Center" />
              <RadioButton Content="Bbbb" FontSize="14" Margin="10,0,0,0" VerticalAlignment="Center" />
            </StackPanel>
          </StackPanel>
        </StackPanel>
      </StackPanel>

      <StackPanel Grid.Row="0" Grid.Column="1" Grid.RowSpan="2"  Orientation="Vertical" HorizontalAlignment="Right">
        <TextBlock Text="MAIN HEADING" FontWeight="Bold" FontSize="38" Padding="0,0,15,0" />
        <TextBlock Text="Additional Information" FontSize="18" />
      </StackPanel>

      <ListBox x:Name="ListBox1" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
        <ListBox.ItemsPanel>
          <ItemsPanelTemplate>
            <toolkit:WrapPanel Orientation="Vertical" />
          </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.Template>
          <ControlTemplate>
            <Grid>
              <ItemsPresenter />
            </Grid>
          </ControlTemplate>
        </ListBox.Template>
      </ListBox>
    </Grid>
  </Canvas>
</UserControl>

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Craig YellickDatabase ArchitectAuthor Commented:
This version allows the WrapPanel to function correctly, and shows all 24 items wrapped as desired. However, becase the Canvas is embedded inside the Grid/StackPanel the ZIndex has no effect and the error message text appears below the other content.

(Use the same ListBox filling code from previous.)

 
<UserControl x:Class="CanvasProblem.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"  
  >
  <Grid >
    <Grid.ColumnDefinitions>
      <ColumnDefinition/>
      <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="30"/>
      <RowDefinition Height="70"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <StackPanel Grid.Row="0" Grid.Column="0" Orientation="Horizontal">
      <HyperlinkButton HorizontalAlignment="Left" Content="Link1" FontSize="14" Margin="2,0" />
      <HyperlinkButton HorizontalAlignment="Left" Content="Link2" FontSize="14" Margin="2,0" />
      <HyperlinkButton HorizontalAlignment="Left" Content="Link3" FontSize="14" Margin="2,0" />
     </StackPanel>

    <StackPanel Grid.Row="1" Grid.Column="0" Orientation="Vertical" >
      <Canvas x:Name="ErrorMessageCanvas">
        <Border  x:Name="ErrorMessageBorder" Width="500" Canvas.Top="50" Canvas.Left="150"
                            Background="Red" BorderThickness="5" CornerRadius="10">
          <TextBlock x:Name="ErrorMessageTextBlock"
                     Text="This is an Error Message and should be displayed on top of all other content."   
                     Padding="6" Margin="6"
                     FontSize="18" FontWeight="Bold" FontStyle="Italic" TextWrapping="Wrap">
          </TextBlock>
        </Border>
      </Canvas>
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="SUB HEADING" Margin="5,0,0,0" FontSize="32" FontStyle="Italic" FontWeight="ExtraBold" VerticalAlignment="Bottom" />
        <StackPanel Orientation="Vertical" Margin="10,0,0,0">
          <StackPanel Orientation="Horizontal">
            <TextBlock Text="More Information" FontSize="14" VerticalAlignment="Center" />
            <TextBlock  Text="(Message)" Margin="6,0,0,0" VerticalAlignment="Center" FontSize="14" FontWeight="Bold"/>
          </StackPanel>
          <StackPanel Orientation="Horizontal">
            <TextBlock Text="Sort by:" FontSize="14" VerticalAlignment="Center" />
            <RadioButton Content="Aaaa" FontSize="14" Margin="10,0,0,0" VerticalAlignment="Center" />
            <RadioButton Content="Bbbb" FontSize="14" Margin="10,0,0,0" VerticalAlignment="Center" />
          </StackPanel>
        </StackPanel>
      </StackPanel>
    </StackPanel>

    <StackPanel Grid.Row="0" Grid.Column="1" Grid.RowSpan="2"  Orientation="Vertical" HorizontalAlignment="Right">
      <TextBlock Text="MAIN HEADING" FontWeight="Bold" FontSize="38" Padding="0,0,15,0" />
      <TextBlock Text="Additional Information" FontSize="18" />
    </StackPanel>

    <ListBox x:Name="ListBox1" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
      <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
          <toolkit:WrapPanel Orientation="Vertical" />
        </ItemsPanelTemplate>
      </ListBox.ItemsPanel>
      <ListBox.Template>
        <ControlTemplate>
          <Grid>
            <ItemsPresenter />
          </Grid>
        </ControlTemplate>
      </ListBox.Template>
    </ListBox>
  </Grid>
</UserControl>

Open in new window

0
 
Craig YellickDatabase ArchitectAuthor Commented:
Outstanding! Thank you. Works perfectly.
0
 
Craig YellickDatabase ArchitectAuthor Commented:
Outstanding solution. Easy to understand and works exactly as desired. Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.