bircastri
bircastri

Reputation: 2159

Change the ZIndex from two element

I have a simple WPF application with two button. This is the code in my xaml file:

<Grid x:Name="grigliaPulsantiBody" Visibility="Visible"
        Grid.Column="1" Grid.Row="0" Panel.ZIndex="2"
        HorizontalAlignment="Center" VerticalAlignment="Center"
        Width="Auto" Height="Auto">
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <!--PRIMA RIGA-->
    <Button x:Name="buttonLookNao" Content="Guarda NAO negli occhi" 
        Grid.Column="0" Grid.Row="0"
        Click="button_Click" Width="200" Height="100" Background="Red" />

    <Button x:Name="buttonLookParent" Content="Guarda Genitore negli occhi" 
        Grid.Column="1" Grid.Row="1"
        Click="button_Click" Width="200" Height="100" Background="Green"/>
    <!--FINE PRIMA RIGA-->
</Grid>

This is another control that I have create:

<local:RiepilogoEsercizio x:Name="riepilogoEsercizio" VerticalAlignment="Top" HorizontalAlignment="Right"
   Margin="0,-600,180,0" Panel.ZIndex="0"
   Grid.Row="0" Grid.Column="0" Visibility="Hidden"/>

If I click on a button, the elemt "riepilogoEsercizio" change his ZIndex, so I have this

 Canvas.SetZIndex(riepilogoEsercizio, 999);

This is ok. Now I want to set hidden this element. I use this code:

private void annullaBoxUscita(object sender, RoutedEventArgs e)
{
    try
    {
    myLoadBoxRiepilogoAnimation.From = 1.0;
    myLoadBoxRiepilogoAnimation.To = 0.0;
    //mostro la finestra dei risultati dell esercizio
    Storyboard.SetTargetName(myLoadBoxRiepilogoAnimation, riepilogoEsercizio.Name);
    Storyboard.SetTargetProperty(myLoadBoxRiepilogoAnimation, new PropertyPath(UIElement.OpacityProperty));
    //riepilogoEsercizio.labelTempo.Content = gioco.tempoEsecuzioneEsercizio;
    //riepilogoEsercizio.labelRisposteEsatte.Content = gioco.numeroRisposteCorrette;
    //riepilogoEsercizio.labelTentativi.Content = gioco._numeroTentativi;
    //riepilogoEsercizio.Visibility = Visibility.Visible;

    Canvas.SetZIndex(riepilogoEsercizio, -800);
    Canvas.SetZIndex(grigliaPulsantiBody, 2);
    myStoryboardBoxRiepilogo.Begin(this);
    riepilogoEsercizio.IsEnabled = false;
    }
    catch (Exception exc)
    {
    log.Error("ButtonEsci_Click: ", exc);
    this.Close();
    Process.GetCurrentProcess().Kill();
    }
}

This code works, the element "riepilogoEsercizio" is hidden, but I can't click on second button "buttonLookParent", because the element "riepilogoEsercizio" is hidden but it is on this button. I try to change the ZIndex setting but not found.

Upvotes: 0

Views: 963

Answers (2)

Ra&#250;l Ota&#241;o
Ra&#250;l Ota&#241;o

Reputation: 4760

The problem here is that you have different panels, you have the Canvas and you have the Grid. In the grid you have the buttons, and in the canvas the controll you want to show/hide. The Panel.Zindex property works for the items that are in the same panel, so in this case decreasing the zindex value will doesn't work. A solution fo this is setting the Visibility property to Collapsed (like :Nikhil Agrawal said) in this way the visual item will behave like if it was not in the visual tree.

Hole this helps...

Upvotes: 0

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48568

Set the Visibility to Collapsed.

Upvotes: 2

Related Questions