Solved

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

Posted on 2010-11-20
6
1,109 Views
Last Modified: 2013-11-12
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.

0
Comment
Question by:CraigYellick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 14

Expert Comment

by:Binuth
ID: 34187421
can you post sample code/app ?
0
 
LVL 11

Author Comment

by:CraigYellick
ID: 34218831
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
 
LVL 11

Author Comment

by:CraigYellick
ID: 34218850
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 7

Accepted Solution

by:
vbigham earned 500 total points
ID: 34249952
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
 
LVL 11

Author Closing Comment

by:CraigYellick
ID: 34250026
Outstanding! Thank you. Works perfectly.
0
 
LVL 11

Author Comment

by:CraigYellick
ID: 34250032
Outstanding solution. Easy to understand and works exactly as desired. Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question