开发者

How to alter a visual state of a ControlTemplate without replacing it?

开发者 https://www.devze.com 2022-12-09 18:55 出处:网络
I want to define a ToggleButton that appears with red foreground (for example) when checked, rather than the default appearance of a pressed button.

I want to define a ToggleButton that appears with red foreground (for example) when checked, rather than the default appearance of a pressed button.

I see that the visual states are maintained as named elements inside the ToggleButton's ControlTemplate.

What is the e开发者_如何转开发asiest way to replace/remove/customize a visual state WITHOUT redefining the entire template?


I realise this is very late, but I just ran into the same issue and figured out the solution myself, so hopefully this will be useful to someone.

After the template has been applied to a control (after the first measure/display cycle), you can access the instantiated template and its child components simply by using VisualTreeHelper.GetChild (myToggleButton, 0) - this will return the root object of the instantiated template's visual tree, from which you can add/remove/modify the child objects in its Children collection.

Obviously you're only changing the visual state of that instance of the control, not the template resource itself, so new controls created from the same template will still have the original visual appearance.

My realted question on this subject is here: How do I replace a ControlTemplate part at runtime, in Silverlight 3?


Have you tried creating your own ControlTemplate and set the BasedOn parameter to standard and then only changed what you needed?

0

精彩评论

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