Wpf文本选框后面的其他元素

时间:2015-10-16 09:23:07

标签: c# wpf canvas text marquee

我构建了一个带有动画文字的消息栏, https://github.com/SonarSource/sonar-.net-documentation/blob/master/doc/analyze-from-tfs.md。 就像你可以看到文字" fly' s"在前台并隐藏占位符。但我需要占位符在前台。

我的第一个想法是改变动画的区域

doubleAnimation.To = tbInfo.ActualWidth *-1;

doubleAnimation.To = boLogo.ActualWidth;

但结果如下:see the .gif from the animation

如何在前台设置占位符,以便动画"飞行"背后呢?

我的XAML代码

<Canvas x:Name="canMain" HorizontalAlignment="Stretch" VerticalAlignment="Center">
    <Border x:Name="boLogo" Height="40" Background="Gray" Canvas.Left="0" Canvas.Top="-20">
        <Button Content="Placeholder" Width="90" />
    </Border>
    <TextBlock x:Name="tbInfo" Visibility="Hidden" FontSize="32" FontWeight="Bold"  Padding="5" HorizontalAlignment="Stretch" VerticalAlignment="Center"></TextBlock>
</Canvas>

以及显示窗口的代码

public void ShowWindow(string str)
{
    tbInfo.Text = str;
    this.Height = 39;
    this.Width = SystemParameters.WorkArea.Width;
    this.Left = SystemParameters.PrimaryScreenWidth - this.Width;
    this.Show();

    TextMarquee(20);
}

private void TextMarquee(int duration)
{
    double height = canMain.ActualHeight - tbInfo.ActualHeight;
    tbInfo.Margin = new Thickness(0, height / 2, 0, 0);
    DoubleAnimation doubleAnimation = new DoubleAnimation();
    doubleAnimation.From = canMain.ActualWidth;
    doubleAnimation.To = tbInfo.ActualWidth * -1;
    doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
    doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(duration));
    tbInfo.BeginAnimation(Canvas.LeftProperty, doubleAnimation);
    tbInfo.Visibility = Visibility.Visible;
}

2 个答案:

答案 0 :(得分:2)

使用Panel.ZIndex:

<Canvas x:Name="canMain" >
<Border x:Name="boLogo" Panel.ZIndex="2">
    <Button Content="Placeholder" Width="90" />
</Border>
<TextBlock x:Name="tbInfo" Panel.ZIndex="1"></TextBlock>
</Canvas>

https://msdn.microsoft.com/de-de/library/system.windows.controls.panel.zindex%28v=vs.110%29.aspx

答案 1 :(得分:1)

尝试使用Grid.ZIndex:

<Grid x:Name="canMain" >
    <Border x:Name="boLogo" Grid.ZIndex="2">
        <Button Content="Placeholder" />
    </Border>
    <TextBlock x:Name="tbInfo" Grid.ZIndex="1"/>
</Grid>

ZIndex = "2"作为最明显的图层。