How to use MultiBinding many times, in abbreviated form, each with different ConverterParameter?

StackOverflow https://stackoverflow.com/questions/21233022

سؤال

I have a IMultiValueConverter called Placer, being use like this:

<Rectangle Name="HostBox" Fill="#FFF4F4F5" Height="36" Stroke="Black" Canvas.Top="32" 
            Width="86" RadiusY="9.5" RadiusX="9.5">
    <Canvas.Left>
        <MultiBinding Converter="{StaticResource Placer}" ConverterParameter="0.5">
            <Binding Path="ActualWidth" RelativeSource="{RelativeSource AncestorType={x:Type Canvas}}"/>
            <Binding Path="Width" RelativeSource="{RelativeSource Self}"/>
        </MultiBinding>
    </Canvas.Left>
</Rectangle>

But I have many Rectangles, on which I want to apply the same logic, but with different ConverterParameter value. Do I have to include this not-so-small snippet under each Rectangle's Canvas.Left attached property? (rhetorical question... obviously there's a smarter way... but how?)

هل كانت مفيدة؟

المحلول

Try using a style. For instance, the following one is applied to all the rectangle instances but you could also give it a key and apply it individually to your rectangles:

    <Style TargetType="Rectangle">
        <Setter Property="Canvas.Left">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource Placer}" ConverterParameter="0.5">
                    <Binding Path="ActualWidth" RelativeSource="{RelativeSource AncestorType={x:Type Canvas}}"/>
                    <Binding Path="Width" RelativeSource="{RelativeSource Self}"/>
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>

In order to parameterize MultiBinding.ConverterParameter you may simply use a binding.

EDIT: I stand corrected about binding to MultiBinding.ConverterParameter: it is not possible since it is not a DependencyProperty but you can work around it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top