开发者

WPF: Is it possible to add or modify bindings via styles or something similar?

开发者 https://www.devze.com 2022-12-27 04:00 出处:网络
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 wa

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 contr开发者_运维问答ols 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?

Alternatively, is it possible to add the binding in the style itself?

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 :-).


You simply cannot use styles to change properties on a Binding. What you can do instead is use the following form to make things prettier:

<TextBox Style="{StaticResource myStyle}"
         Text="{Binding SomePath,Source=SomeSource,ValidatesOnDataErrors=True,ValidatesOnExceptions=True,UpdateSourceTrigger=PropertyChanged}" />

One other thing you can do is derive a class from Binding and use that as a custom markup extension. In your derived class constructor you can set all the common defaults like PropertyChanged for UpdateSourceTrigger, etc.


You can specify binding inside a datatemplate. Problem is getting that datatemplate into the textbox that you want. So, instead what you do is set the data template to include a textbox, and then set a ContentControl's content to that static resource template, like so:

<DataTemplate x:Key="BoundTextbox">
    <TextBlock TextAlignment="Right" VerticalAlignment="Center" TextWrapping="WrapWithOverflow">
        <TextBlock.Text>
            <Binding Path="Path" Source="Source"
                ValidatesOnDataErrors="True"
                ValidatesOnExceptions="True"
                UpdateSourceTrigger="PropertyChanged" />
        </TextBlock.Text>
    </TextBlock>
</DataTemplate>

<ContentControl ContentTemplate="{StaticResource BoundTextbox}" Content="" />

The last line is what you need to do put in your XAML usercontrol/page.

But be sure to add the datatemplate to your resource dictionary so you can reference it as a static resource in your usercontrol/page.

0

精彩评论

暂无评论...
验证码 换一张
取 消