开发者

WPF - Is it possible to assign an event in a Style?

开发者 https://www.devze.com 2023-03-06 22:31 出处:网络
Specifically I want all PasswordBox\'s using a Style to have the same static KeyDown event handler - would it be pos开发者_如何学JAVAsible to somehow set this in the Style they are using rather than s

Specifically I want all PasswordBox's using a Style to have the same static KeyDown event handler - would it be pos开发者_如何学JAVAsible to somehow set this in the Style they are using rather than setting it on each PasswordBox? (which I am having to do in code behind at the moment as they all point to the same static handler)..


EventSetter is your friend. As for the handler - just put it into code-behind (you can create a code-behind for a resource dictionary too - just crate a CS file with the same name as the dictionary and add the class attribute to the resource dictionary XAML).

Small example:

<Style>
    <EventSetter Event="KeyDown" Handler="KeyDownHandler"/>
</Style>
0

精彩评论

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