Add code to a style in WPF

2019-08-11 19:23发布

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

2条回答
SAY GOODBYE
2楼-- · 2019-08-11 19:54

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

查看更多
走好不送
3楼-- · 2019-08-11 20:13
<Style x:Key="MyStyle">
    <EventSetter Event="Control.GotFocus" Handler="Control_GotFocus"></EventSetter>
</Style>
查看更多
登录 后发表回答