Click here to Skip to main content
15,891,847 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi. i'm facing problem in achieving below things using WPF, XAML / c# -
1. Need to hide a textblock for 3 seconds and show it for 2 seconds on a window
2. It's position should be random every time it shows on the window.
Please help me out..

What I have tried:

I tried following but it's giving desired result -

<TextBlock Foreground="Red" Text="Some Information">
	<TextBlock.Triggers>
		<EventTrigger  RoutedEvent="Window.Loaded">
			<BeginStoryboard>
				<Storyboard>
					<DoubleAnimation Storyboard.TargetProperty="(TextBlock.Opacity)"
						From="1" 
						To="0" 
						AutoReverse="True"
						BeginTime="0:0:0" 
						Duration="0:0:.125"
						RepeatBehavior="Forever">
					</DoubleAnimation>
				</Storyboard>
			</BeginStoryboard>
		</EventTrigger>
	</TextBlock.Triggers>
</TextBlock>
Posted
Updated 20-Apr-17 12:50pm

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Timer tmr;
        public MainWindow ( )
        {
            InitializeComponent ( );
            tmr = new Timer {Interval = 3*1000};
            tmr.Elapsed +=tmr_Elapsed;
            tmr.Start ( );
        }

        private void tmr_Elapsed (object sender , ElapsedEventArgs e)
        {
            Application.Current.Dispatcher.Invoke ( GetValue );
        }

        private void GetValue()
        {
            if (tmr.Interval == 3*1000)
            {
                txtSample.Visibility = Visibility.Collapsed;
                tmr.Interval = 2*1000;
            }
            else
            {
                txtSample.Visibility = Visibility.Visible;
                tmr.Interval = 3*1000;
            }
        }
    }
}
 
Share this answer
 
You should use the Visibility property to show and hide the control. That said, you had the right idea going for the EventTrigger. Use ObjectAnimationUsingKeyFrames for the Visibility property. After 2 seconds the TextBlock will collapse (become hidden), then 3 seconds later it will become visible again.

<TextBlock.Triggers>
	<EventTrigger  RoutedEvent="Window.Loaded">
		<BeginStoryboard>
			<Storyboard>
                <ObjectAnimationUsingKeyFrames 
                    Storyboard.TargetProperty="Visibility"
                    RepeatBehavoir="Forever">
                    <DiscreteObjectKeyFrame KeyTime="0:0:2" 
                        Value="{x:Static Visibility.Collapsed}"/>
                    <DiscreteObjectKeyFrame KeyTime="0:0:5" 
                        Value="{x:Static Visibility.Visible}"/>
                </ObjectAnimationUsingKeyFrames>
			</Storyboard>
		</BeginStoryboard>
	</EventTrigger>
</TextBlock.Triggers>


For the random positioning TextBlock has an IsVisibleChanged event that you can use. I added this C# code to that event. The event will trigger every time the storyboard changes the Visiblity. Note that I didn't bother updating the position in a separate thread. This is because when the storyboard changes the Visibility again the control has to be re-rendered and the new position values are used when that happens.

private void UpdatePostion(object sender, DependencyPropertyChangedEventArgs e)
{
    FrameworkElement tb = (FrameworkElement)sender;
    Random rnd = new Random();
    double tbHeight = tb.Height;
    double tbWidth = tb.width;
    Thickness tbMargin = tb.Margin;

    // mygrid is the name of the container my TextBlock is in
    double GridHeight = myGrid.ActualHeight;
    double GridWidth = myGrid.ActualWidth;

    // only update postion when the control is collapsed
    if (tb.Visibility != Visibility.Collapsed)
        return;

    tbMargin.Top = (double)(rnd.Next(0, (int)(GridHeight - tbHeight)));
    tbMargin.Width = (double)(rnd.Next(0, (int)(GridWidth - tbWidth)));
    tb.Margin = tbMargin;
}
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900