Normal 0 false false false MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;}
We are trying to change the background on DataRecordPresenter to animate on the basis of a DataTrigger. The data trigger is firing but our problem is how to get at the Background property to change it. It takes a Brush but the ColorAnimationUsingKeyFrames returns a SolidColorBrush?
I am getting the exception "'System.Windows.Media.Animation.ColorAnimationUsingKeyFrames' animation object cannot be used to animate property 'Background' because it is of incompatible type 'System.Windows.Media.Brush'." with the below code.
<Storyboard x:Key="storyboard1">
<ColorAnimationUsingKeyFrames
Storyboard.TargetProperty="Background"
Duration="0:0:6"
FillBehavior="HoldEnd" RepeatBehavior="Forever">
<ColorAnimationUsingKeyFrames.KeyFrames>
<LinearColorKeyFrame Value="Red" KeyTime="0:0:2" />
<DiscreteColorKeyFrame Value="Yellow" KeyTime="0:0:2.5" />
<SplineColorKeyFrame Value="Green" KeyTime="0:0:4.5" KeySpline="0.6,0.0 0.9,0.00" />
</ColorAnimationUsingKeyFrames.KeyFrames>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Style TargetType="{x:Type igDP:DataRecordPresenter}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=DataItem.IsNew}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource storyboard1}" x:Name="startOn"/>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<StopStoryboard BeginStoryboardName="startOn" />
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
We were thinking that the target property should be something like
(Control.Background).(SolidColorBrush.Color)
Please can you point us in the right direction?
Hello,
I was very happy to see someone ask a question regarding Animations, as I am a big fan of them. I have struggled for and hour, or two with the style and storyboards you have give me, but I just cannot make the data trigger fire. I reproduced a couple of scenarios that are close to yours avoiding using the DataTrigger and the Animation works for me nicely -- just instead of
Storyboard.TargetProperty="Background" I used : Storyboard.TargetProperty="Background.Color". As the DataTrigger fires properly in your application this should be enough to finish it, if not, please give me some more source, or part of the application (or any other like it) and I will be happy to spend some more hours tackling the problem.
Alex
Alex,
Thanks that helped and you answered quickly as well, but I still ran into problems.
Your answer closed off what the property item syntax was for the Target. I got it to work, still using a DataTrigger.
The source code I have is rather large. In summary, to get the DataTrigger to fire when DataItem.IsNew = true, is just on a object that implements INotifyPropertyChanged like this:
public sealed class Dummy : INotifyPropertyChanged { private bool? m_isNew = true;
public bool? IsNew { get { return m_isNew; } set { if (value != m_isNew) { m_isNew = value; OnPropertyChanged("IsNew"); }
} }
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) { var propertyChangedEventArgs = new PropertyChangedEventArgs(propertyName);
if (PropertyChanged != null) { PropertyChanged(this, propertyChangedEventArgs); } }
#endregion
}
The xamDataGrid is then just bound to a ObservableCollection<Dummy> instance.
In the Xaml, after changing the target from "Background" to "Background.Color" I still kept getting this error when an item was added to the grid:
"Cannot resolve all property references in the property path 'Background.Color'. Verify that applicable objects support the properties."
I then changed the xaml so the DataTrigger only fires when IsNew = False. Not intuitive but just testing. The XamDataGrid then displayed correctly as the storyboard is not activated.
If I modify the IsNew property to false and move off the item in the grid to save changes, the IsNew is then changed to false and the storyboard1 runs and colors the background. Great!
Then if I change tabs so the grid is no longer displayed, then select the tab again with the grid, I get the same error message again.
If I continue, I then get this error message:
"The previous newly realized generated element was not added to the visual tree by calling either the RecyclingItemsPanel's AddInternalChild or InsertInternalChild method."
Is sounds like because the grid is no longer in focus, the grid is being recycled and the SoldColourBrush that is being animated is causing it to crash, because the Background brush is being replaced with a storyboard.
Any ideas? I turned off the XamDataGrid recycling as you can see, but no effect. I would have thought this should just work....
I can think of many uses of a DataTrigger to draw users attention when something happens but it seems not easy to do.
Another question is how do you decode what the Property Item syntax should be for a TargetProperty? Playing with the objects in C#, using Mole/Snoop and searching seems to be the only way. Blend perhaps? It would be nice to have a debug Xaml element that halts execution when Xaml is fired to allow you to inspect what objects are there to explore the context they are running in or something like that.....
I just reproduced your scenario and got the same exception. I was circling around the background property, but now it makes sense in some way -- the property itself is not initialized or takes incorect value. For example, when you apply some kind of Transform to an element (layout or render) first you have to "initialize" it with some default value in order to be able to actually apply the transformation later,like:
<Ellipse> <Ellipse.RenderTransform> <TranslateTransform X="0" Y="0"/> </Ellipse.RenderTransform> </Ellipse>
and then in the Storyboard for example TargetProperty="(Ellipse.RenderTransform).(TranslateTransform.X)"
I think this is the case here but I am not quite sure. Great job by the way, it looks great.
Thanks,
Alex.
we are also looking to implement similar functionality - is there a sample project for this post?
What exactly are you trying to implement and which is the part you have difficulties with?
Regards,
The Same Here ... I've Already Done The Animation Part for The Background of a record .. Like This:
<Window.Resources> <Storyboard AutoReverse="True" x:Key="storyboard1"> <ColorAnimation Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)" From="White" To="Red" Duration="0:0:1" /> </Storyboard> <!--DataRecordCellArea--> </Window.Resources> <Grid> <InfraDP:XamDataGrid Name="dg_test" CellActivated="dg_test_CellActivated" CellChanged="dg_test_CellChanged" CellUpdated="dg_test_CellUpdated" FieldLayoutInitialized="dg_test_FieldLayoutInitialized" FieldPositionChanging="dg_test_FieldPositionChanging" > <InfraDP:XamDataGrid.Resources> <Style TargetType="{x:Type InfraDP:CellValuePresenter}"> <Setter Property="Background" Value="White" /> <Style.Triggers> <DataTrigger Binding="{Binding Mode=TwoWay, RelativeSource={RelativeSource Self}, Path=Record.DataItem.IsChanged}" Value="True"> <DataTrigger.EnterActions> <BeginStoryboard Storyboard="{StaticResource storyboard1}"> </BeginStoryboard> </DataTrigger.EnterActions> </DataTrigger> </Style.Triggers> </Style> </InfraDP:XamDataGrid.Resources>
and It Works Fine Since I implemented the INotifyPropertyChanged Interface in my Data class .. Now My Problem Is :
can we set "Record.DataItem.IsChanged" back to false in order to make the trigger activate once more once the "IsChanged" is Set To True ?? Please Help.
Yours:
Gamil.
This is Through XAML ofCourse .. Not Code .. Since I Want To Make Use of The DataTrigger itself in doing such thing ... The Summary of All of This:
Can we set DataItem.IsChanged Property back To True Once The Trigger is Fired .. in XAML .. Many Thanks To You.