1

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?)

Tar
  • 8,529
  • 9
  • 56
  • 127
  • Just a thought, but it might perhaps be much easier to implement a [custom panel](http://msdn.microsoft.com/en-us/library/ms754152.aspx#Panels_custom_panel_elements) with special layout behaviour, as for example shown in [this answer](http://stackoverflow.com/a/21158560/1136211). – Clemens Jan 20 '14 at 15:00

1 Answers1

1

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.

jnovo
  • 5,659
  • 2
  • 38
  • 56
  • But then I cannot change the `ConverterParameter` – Tar Jan 20 '14 at 12:21
  • How do I parameterize `MultiBinding.ConverterParameter` that's inside a `Style`, using binding in the target `Rectangle`? – Tar Jan 20 '14 at 12:57
  • Once the style is applied to an element, the bindings apply to that element's `DataContext`. Thus, you can bind to an appropriate View Model property. – jnovo Jan 20 '14 at 14:00
  • Doesn't feel right to mix pure appearance (`View`), with `viewModel` – Tar Jan 20 '14 at 15:10
  • I just assumed you had some logic behind that parameter value in your View Model. Of course, you may bind it to anything within your logic tree.. – jnovo Jan 20 '14 at 15:15