开发者

WPF, trigger, style scope and declarative programming model question

开发者 https://www.devze.com 2023-01-27 07:32 出处:网络
TL;DR Xaml feels opaque to me, why did removing TargetName from an animation fail in one case and work in another for the exac开发者_开发技巧t same (copy and pasted) Trigger declaration?

TL;DR Xaml feels opaque to me, why did removing TargetName from an animation fail in one case and work in another for the exac开发者_开发技巧t same (copy and pasted) Trigger declaration?

In This question my problem was solved by moving the trigger into a style and applying the style to the text boxes. From the example you can see the storyboard has a TargetName property set to the name of one of the text boxes.

While trying to make this work I had also put the trigger in the Grid.Triggers section and removed the TargetName property. This caused an exception to be thrown.

The solution involved putting it in a style and then removing the TargetName property setter. The style wasn't named, but applied to text boxes. In this case, with TargetName not set, it worked.

So here is the question. It took me a bit of fiddling to get it to work, renaming TargetName seemed sensible, as I wanted it to apply to several text boxes, but I had tried that when I put it in the Grid.Triggers section. Why didn't that work, and why did it work in a style? More importantly, how do I use that knowledge to solve similar problems later on? That's the tricky bit I think, Xaml seems quite opaque to me at the moment.


Storyboard.TargetName is not about applying a behavior; it is a portion of the execution of the behavior. Styles are typically about applying a predefined behavior across varying instances which may include a Storyboard and potentially a Storyboard.TargetName.

In addition styles can often be inline or exterior as a referenced resource. It can depend on how the style is being used, ie...is it a one off for that specific control or is this something we want propagated throughout the application?

Understanding the concepts up front will prevent hours of troubleshooting later on. There are many great articles out there around WPF and its concepts, as well as many great books.

0

精彩评论

暂无评论...
验证码 换一张
取 消