So forgive me ahead of time if this has been answered before and I just haven't been able to see it for the life of me, but I have been searching for the past 4 hours with no avail.
I am trying to get a DataTrigger to fire on the change of a single bool property. The DataTrigger is to begin a storyboard that simply just adjusts the window size.
Here is the XAML:
<Window.Resources>
<local:SatisfactionsViewModel x:Key="SatisfactionsViewModel"/>
<!--The storyboard animation-->
<Storyboard x:Key="windowFadeUp">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="Satisfaction_Processing">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="171"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<!--The Datatrigger that doesn't seem to work, probably my own stupidity-->
<Window.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsWorkingOnStuff, NotifyOnSourceUpdated=True}" Value="true">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource windowFadeUp}"/>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Style>
Here is the relevant C# from the ViewModel:
private bool isWorkingOnStuff;
public bool IsWorkingOnStuff
{
get { return isWorkingOnStuff; }
set
{
if (value == isWorkingOnStuff)
{ return; }
isWorkingOnStuff = value;
OnPropertyChanged();
}
}
//Other non-relative stuff
//PropertyChangedEvent handler
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
//Bunch of other stuff not related
private void ProcessCommand()
{
if (fileName == null || fileName == "")
{
MessageBox.Show("Please select a file. \nIt won't work without one... -_-");
}
else
{
IsWorkingOnStuff = true;
IsProcessEnabled = false;
IsProgressBarVisibile = Visibility.Visible;
//more business logic goes here...
}
}
So as I understand it, when the IsWorkingOnStuff property changes to true, the datatrigger should fire, the storyboard execute, and rejoicing happen...but it's not.
Any idea where I am going wrong?
Thank you ahead of time.
Remove the Storyboard.TargetName
attribute and make sure that you have set the DataContext
of the window to an instance of the SatisfactionsViewModel
class:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300" x:Name="Satisfaction_Processing">
<Window.DataContext>
<local:SatisfactionsViewModel />
</Window.DataContext>
<Window.Resources>
<!--The storyboard animation-->
<Storyboard x:Key="windowFadeUp">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="171"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsWorkingOnStuff, NotifyOnSourceUpdated=True}" Value="true">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource windowFadeUp}"/>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Style>
<StackPanel>
</StackPanel>
</Window>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.