开发者

WPF: Canvas and zIndex? How does it work?

开发者 https://www.devze.com 2023-01-29 08:05 出处:网络
I have the following layou: <s:SurfaceWindow x:Class=\"Prototype_Concept_2.SurfaceWindow1\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"

I have the following layou:

<s:SurfaceWindow x:Class="Prototype_Concept_2.SurfaceWindow1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="http://schemas.microsoft.com/surface/2008"
    Title="Prototype_Concept_2"
    >
  <s:SurfaceWindow.Resources>
    <ImageBrush x:Key="WindowBackground" Stretch="None" Opacity="0.6" ImageSource="pack://application:,,,/Resources/WindowBackground.jpg"/>
  </s:SurfaceWindow.Resources>

  <Grid Background="{StaticResource WindowBackground}" >

            <Grid Name="ProjectsGrid">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="Auto"></RowDefinition>

                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <TextBox Name="P开发者_C百科rojectsHeader" Grid.ColumnSpan="2" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="25" Text="Please choose one of the following projects" Grid.Row="0"></TextBox>
                <s:SurfaceButton Name="BottomButton" HorizontalAlignment="Right" FontSize="20" Width="100" Grid.Column="1" Grid.Row="2" Foreground="White" Content="Refresh"></s:SurfaceButton>
                <s:SurfaceListBox Background="Black" Grid.ColumnSpan="2" Name="ProjectsList" Grid.Row="1" ItemsSource="{Binding Projects}"></s:SurfaceListBox>
                <Label Name="ProjectsFooter" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" FontSize="15" Content="Fetching projects data ..."></Label>

            </Grid>

        <Grid ShowGridLines="True"  Name="SmellHeader" Visibility="Collapsed">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="256"></ColumnDefinition>
            <ColumnDefinition Width="256"></ColumnDefinition>
            <ColumnDefinition Width="256"></ColumnDefinition>
            <ColumnDefinition Width="256"></ColumnDefinition>
        </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="38"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                </Grid.RowDefinitions>

            <Rectangle Grid.Column="0" Grid.Row="0" Fill="Black"></Rectangle>
            <Viewbox Grid.Column="0" Grid.Row="0" s:Contacts.PreviewContactDown="BrainClass_PreviewContactDown">
                <Label Background="Black" Foreground="White" Content="BrainClass" HorizontalContentAlignment="Center"></Label>
            </Viewbox>

            <Rectangle Grid.Column="1" Grid.Row="0" Fill="Black"></Rectangle>
            <Viewbox Grid.Column="1" Grid.Row="0">
                <Label Background="Black" Foreground="White" Content="God Class" HorizontalContentAlignment="Center"></Label>
            </Viewbox>

            <Rectangle Grid.Column="2" Grid.Row="0" Fill="Black"></Rectangle>
            <Viewbox Grid.Column="2" Grid.Row="0">
                <Label Background="Black" Foreground="White" Content="Tradition Breaker" HorizontalContentAlignment="Center"></Label>
            </Viewbox>

            <Rectangle Grid.Column="3" Grid.Row="0" Fill="Black"></Rectangle>
            <Viewbox Grid.Column="3" Grid.Row="0">
                <Label Background="Black" Foreground="White" Content="RefusedParent Bequest" HorizontalContentAlignment="Center"></Label>
            </Viewbox>
        </Grid>
        <Canvas Name="RootLayer" Grid.Row="1" Grid.ColumnSpan="4">
        </Canvas>
    </Grid>
</s:SurfaceWindow>

To the RootLayer I add some Ellipse. Later I want to reoder them:

internal void setFocus(SourceManager manager)
        {
            Console.WriteLine("Set focus to class " + getFullName());

            foreach (SourceFile sf in manager.getBrainClasses())
            {
                sf.getVisualizer().Fill = Brushes.Red;
                Canvas.SetZIndex(sf.getVisualizer(), 0);
            }

            this.getVisualizer().Fill = Brushes.Blue;

            Canvas.SetZIndex(this.getVisualizer(), 1);

            manager.window.RootLayer.InvalidateArrange();
            manager.window.RootLayer.InvalidateVisual();
        }

The Ellipse is referenced by this.getVisualizer(); However, nothing changes? How can I bring one Ellipse to the front?


It's not fully clear from your code post how the ellipses are added to the canvas but here is a small sample that does essentially what you want to do:

<Grid>
  <Canvas x:Name="RootLayer" Width="500" Height="500" />
</Grid>

And in the constructor of the code behind, create some ellipses:

for (int i = 0; i < 10; i++)
{
  Ellipse e = new Ellipse
    {
      Width = 100,
      Height = 100,
      Fill = new SolidColorBrush(
               Color.FromArgb(0xDD, 
                    (Byte) r.Next(255)
                    (Byte) r.Next(255)
                    (Byte) r.Next(255))),
      Stroke = Brushes.Black,
      StrokeThickness = 1,
    };
  e.MouseUp += new MouseButtonEventHandler(e_MouseUp);
  Canvas.SetLeft(e, r.Next(400));
  Canvas.SetTop(e, r.Next(400));
  RootLayer.Children.Add(e);
}

Event handler to handle mouse click on the ellipses

void e_MouseUp(object sender, MouseButtonEventArgs e)
{
  foreach (UIElement item in RootLayer.Children)
    Panel.SetZIndex(item, 0);

  Panel.SetZIndex((UIElement)sender, 1);
}

With the code above, whenever an ellipse is clicked (mouse up), it will raise above all the other ellipses in that canvas.

0

精彩评论

暂无评论...
验证码 换一张
取 消