1

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

H.B.
  • 166,899
  • 29
  • 327
  • 400
David Brunelle
  • 6,528
  • 11
  • 64
  • 104
  • Depends on what you want to do in the got focus event. If you want to change the textbox's appearance, then you can do that with styles and triggers. If you want to do something else, then it depends on what that "something else" is. – Joe White Nov 03 '11 at 20:52
  • I really mean codes, like email validations and other stuff. That would be in my lostfocus event though – David Brunelle Nov 03 '11 at 20:57
  • The same applies in WPF as Silverlight. You have to subclass - http://stackoverflow.com/questions/7960708/style-controls-in-silverlight-global/7960740#7960740 – ChrisF Nov 03 '11 at 23:21

2 Answers2

2
<Style x:Key="MyStyle">
    <EventSetter Event="Control.GotFocus" Handler="Control_GotFocus"></EventSetter>
</Style>
Adam Barney
  • 2,377
  • 3
  • 18
  • 24
  • I usually use ressources files to put my styles, and I don't think they have code behind file – David Brunelle Nov 03 '11 at 20:58
  • You would need to have the Event handler somewhere where the EventSetter could see it - Code behind being the most obvious spot, which would mean the event setter would need to be in the Page/Window/UserControl XAML. You could create UserControl or a Custom Control to encapsulate the style and event handling. – Adam Barney Nov 03 '11 at 21:06
  • OK, so I put my style in Application.XAML with only the code and based it on another style and it works perfectly. This is so nice :) Thanks a lot. – David Brunelle Nov 04 '11 at 12:35
0

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

Community
  • 1
  • 1
Sergei B.
  • 3,227
  • 19
  • 18
  • Because I used validation where it might not have been a good word I guess. I don't mean validation in term of data validation, but validation in term of verifying wheter or not a value is selected, or wheter or not the selected value is the same as another combo, etc... – David Brunelle Nov 04 '11 at 12:20