Link to home
Start Free TrialLog in
Avatar of Craig Yellick
Craig YellickFlag for United States of America

asked on

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.

Avatar of Binuth
Binuth
Flag of India image

can you post sample code/app ?
Avatar of Craig Yellick

ASKER

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

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

ASKER CERTIFIED SOLUTION
Avatar of Vaughn Bigham
Vaughn Bigham
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Outstanding! Thank you. Works perfectly.
Outstanding solution. Easy to understand and works exactly as desired. Thanks!