从两个元素更改ZIndex

时间:2015-03-30 15:09:22

标签: c# wpf z-index

我有一个带有两个按钮的简单WPF应用程序。这是我的xaml文件中的代码:

<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>

这是我创建的另一个控件:

<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"/>

如果我点击一个按钮,就会发出电子邮件&#34; riepilogoEsercizio&#34;改变他的ZIndex,所以我有这个

 Canvas.SetZIndex(riepilogoEsercizio, 999);

这没关系。 现在我想隐藏这个元素。我使用这段代码:

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();
    }
}

此代码有效,元素&#34; riepilogoEsercizio&#34;是隐藏的,但是我无法点击第二个按钮&#34; buttonLookParent&#34;,因为元素&#34; riepilogoEsercizio&#34;是隐藏但它在这个按钮上。我尝试更改ZIndex设置但未找到。

2 个答案:

答案 0 :(得分:2)

Visibility设置为Collapsed

答案 1 :(得分:0)

这里的问题是你有不同的面板,你有Canvas而你有Grid。在网格中,您有按钮,在画布中您需要显示/隐藏控件。 Panel.Zindex属性适用于同一面板中的项目,因此在这种情况下,降低zindex值将不起作用。解决这个问题的方法是将Visibility属性设置为Collapsed(如:Nikhil Agrawal所述),这样视觉项就会像在视觉树中一样。

这有助于......