I have a ListBox that uses a custom control as the ListBox.ItemTemplate DataTemplate.
I guess my first question (and the rest are relevant) is how to you create unique names for the children in a listbox?
I ask because I am attempting to invoke a ListBox.Resource Storyboard from codebehind. The error I am getting is that the MyShadow is not found.
<MyControl.BitmapEffect>
<DropShadowBitmapEffect x:Name="MyShadow" ShadowDepth="5" Opacity="0.5" Softness="0.5" />
</MyControl.BitmapEffect>
My xaml storyboard is
<Storyboard x:Key="FocusedShadow">
<!-- Shadow Animation-->
<DoubleAnimation
Storyboard.TargetName="MyS开发者_如何学编程hadow"
Storyboard.TargetProperty="ShadowDepth"
To="15"
Duration="0:0:.14" />
</Storyboard>
I think what I need to do is specify the Targetname as the name of the item (relating back to first question) and the TargetProperty should be MyShadow.Shadowdepth but I don't know how to get there from here.
In code I am doing a storyboardobject.begin(this);
TIA
I had a very similar problem. basically, (I am thinking) that you are trying to animate before the visual tree is fully created. I solved this by using two techniques.
first, I Raise my animate event (which fires the Trigger in my xaml) inside of a Dispatcher.BeginInvoke(...
), using a priority of ApplicationIdle
(this way we are pretty sure the visual tree has been created) also, I am calling UpdateLayout()
before I raise the event.
second, I am accessing the object, like so: var child = this.GetTemplatedChild("MyCanvas")
to ensure that the canvas is actually there before trying to raise my event. (not so sure this helped, but the exception went away after I added this)
here is my sample code:
this.Dispatcher.BeginInvoke(new Action(() =>
{
this.UpdateLayout();
if (this.Visibility == System.Windows.Visibility.Visible)
{
var bzyCanvas = this.GetTemplateChild("BusyCanvas");
if (bzyCanvas != null)
this.RaiseEvent(new RoutedEventArgs(AnimationStarted));
}
}),System.Windows.Threading.DispatcherPriority.ApplicationIdle);
精彩评论