Reducing WPF binding boilerplate with styles - updating the bindings themselves via styling?

Posted by Eamon Nerbonne on Stack Overflow See other posts from Stack Overflow or by Eamon Nerbonne
Published on 2010-04-15T09:39:03Z Indexed on 2010/04/15 9:43 UTC
Read the original article Hit count: 569

Filed under:
|
|
|

I'm still learning the WPF ropes, so if the following question is trivial or my approach wrong-headed, please do speak up... I'm trying to reduce boilerplate and it sounds like styles are a common way to do so. In particular:

I've got a bunch of fairly mundane data-entry fields. The controls for these fields have various properties I'd like to set based on the target of the binding - pretty normal stuff. However, I'd also like to set properties of the binding itself in the style to avoid repetitiveness.

For example:

<TextBox Style="{StaticResource myStyle}">
    <TextBox.Text>
        <Binding Path="..." Source="..."
                 ValidatesOnDataErrors="True"
                 ValidatesOnExceptions="True"
                 UpdateSourceTrigger="PropertyChanged">
        </Binding>
    </TextBox.Text>
</TextBox>

Now, is there any way to use styling - or some other technique to write the previous example somewhat like this:

<TextBox Style="{StaticResource myStyle}" Text="{Binding Source=... Path=...}/>

That is, is there any way to set all bindings that match a particular selection (here, on controls with the myStyle style) to validate data and to use a particular update trigger? Is it possible to template or style bindings themselves?

Clearly, the second syntax is much, much shorter and more readable, and I'd love to be able to get rid of other similar boilerplate to keep my UI code comprehensible to myself :-).

© Stack Overflow or respective owner

Related posts about wpf

Related posts about wpf-binding