简体   繁体   English

如何以缩写形式多次使用MultiBinding,而每个缩写都有不同的ConverterParameter?

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

I have a IMultiValueConverter called Placer , being use like this: 我有一个名为PlacerIMultiValueConverter ,被这样使用:

<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 Rectangle s, on which I want to apply the same logic, but with different ConverterParameter value. 但是我有很多Rectangle ,我想在上面应用相同的逻辑,但是具有不同的ConverterParameter值。 Do I have to include this not-so-small snippet under each Rectangle 's Canvas.Left attached property? 我是否必须在每个RectangleCanvas.Left附加属性下包含这个不太小的代码段? (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. 为了参数化 MultiBinding.ConverterParameter您可以简单地使用绑定。

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. 编辑:关于绑定到MultiBinding.ConverterParameter我已得到纠正:由于这不是DependencyProperty但是您可以解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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