简体   繁体   中英

Using a converter for more than one binding?

I would like to do something like this:

<Style TargetType="{x:Type Binding}">
    <Setter Property="Converter" Value="{StaticResource converter1}"/>
</Style>

That doesn't work though. So how do I tell more than one binding which converter to use without writing it explicitly for every single one?

I'm sorry to say, but there are actually two reasons you cannot do this.

<Style TargetType="{x:Type Binding}">
    <Setter Property="Converter" Value="{StaticResource converter1}"/>
</Style>

Firstly, you cannot create a style for System.Windows.Data.Binding because it does not meet the requirements for styling. The TargetType must derive from either FrameworkElement or FrameworkContentElement . Alas, Binding inherits from BindingBase , then MarkupExtension , then Object and so it cannot be styled.

Secondly, Setter.Property is of type DependencyProperty . Binding.Converter is not a dependency property, so it simply cannot have a value bound to it.

So , you will have to repeat the Converer={StaticResource converter1} within the braces of each XAML {Binding} markup extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM