Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically Begin Storyboard (C#)

Tags:

c#

uwp

i want Storyboard automatically begin if something is changed :- E.g I have text block which can contain text "On" or "Off"

<TextBlock Name="BindedTextBlock" />

for checking text block text is on or off i created DispatcherTimer (Suggest me anyother way if which can check Text block text)

if textblock text is ON then NextStoryBoardIn.Begin(); should begin, if textblock text is OFF then PrevStoryBoardOut.Begin(); should begin. so i done this way:

DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(0) };
timer.Tick += delegate (object sender, object e)
{                
    if(BindedTextBlock.Text.Equals("On"))
    {
        PrevStoryBoardIn.Begin();
    }
    else if(BindedTextBlock.Text.Equals("Off"))
    {
        PrevStoryBoardOut.Begin();
    }
};
timer.Start();

it works fine but storyboard triggers continuously, it should begin once. and if i write

if(BindedTextBlock.Text.Equals("On"))
{
    PrevStoryBoardIn.Begin();
}
else if(BindedTextBlock.Text.Equals("Off"))
{
    PrevStoryBoardOut.Begin();
}
timer.Stop();

then it will never check text block text again even if text block text if updated.

Update

if any one intrested in view Xaml codes for testing purpose , so i shared some sample of my xaml

<Page.Resources>
    <Storyboard x:Name="PrevStoryBoardIn">
        <DoubleAnimation Storyboard.TargetName="AppearStackPanel" Storyboard.TargetProperty="Opacity"
                         From="0" To="1" Duration="0:0:1"/>
    </Storyboard>

    <Storyboard x:Name="PrevStoryBoardOut">
        <DoubleAnimation Storyboard.TargetName="AppearStackPanel" Storyboard.TargetProperty="Opacity"
                         From="1" To="0" Duration="0:0:1"/>
    </Storyboard>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock x:Name="DogWatcherTextBlock"
                   Height="50" Width="100" VerticalAlignment="Top"/>
    <StackPanel x:Name="AppearStackPanel" BorderThickness="1" BorderBrush="Crimson" Height="150" Width="150" Opacity="0" HorizontalAlignment="Center" VerticalAlignment="Center">
        <!-- My Items -->            
    </StackPanel>
</Grid>
like image 301
Arpit Jain Avatar asked Feb 01 '26 05:02

Arpit Jain


1 Answers

Using DispatcherTimer is not a good idea.

There is no TextChanged event for TextBlock. So we can create one and perform your task in it.

public MainPage()
{
    this.InitializeComponent();

    //Register PropertyChangedCallback
    MyTextBlock.RegisterPropertyChangedCallback(TextBlock.TextProperty, OnTextChanged);
}

private void OnTextChanged(DependencyObject sender, DependencyProperty dp)
{
    if(((TextBlock)sender).Text == "On")
        PrevStoryBoardIn.Begin();
    else if(((TextBlock)sender).Text == "Off")
        PrevStoryBoardOut.Begin();
}
like image 75
Vijay Nirmal Avatar answered Feb 02 '26 18:02

Vijay Nirmal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!