Search code examples
wpfxamlstylescode-behind

Add code to a style in WPF


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


Solution

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