Add code to a style in WPF

别来无恙 提交于 2019-12-14 02:28:40

问题


I have several projects in which I have for exemple, textboxes that I would like to behave the same way always. For evertyhing like background to lenght, it's fine, but I also would like to add base event handler (in this case, the got focus event).

Is this possible and if so how ?

Thanks.

Edit : here is an example :

<Style x:Key="BaseComboBox" TargetType="ComboBox">
    <Setter Property="FontSize" Value="12"></Setter>
    <Setter Property="Foreground" Value="Black"></Setter>
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="Height" Value="22"/>
    <Setter Property="Margin" Value="5,0,5,0"/>       
    <Setter Property="IsEditable" Value="True" />
    <Add LostFocus Event that will validate the selection here...>
</Style>

All my styles are in Resources Dictionaries


回答1:


<Style x:Key="MyStyle">
    <EventSetter Event="Control.GotFocus" Handler="Control_GotFocus"></EventSetter>
</Style>



回答2:


Why not to use common approach for validation in your project instead of creating something weird?

Try to read this WPF Data Binding and Validation Rules Best Practices



来源:https://stackoverflow.com/questions/8001619/add-code-to-a-style-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!