none
Bind to margin top property?

    Question

  • Is there any special way to bind to the margin top, bottom, left, or right properties?  In XAML, margin is set via

     

    <Rectangle Margin="0,5,14,7" .... />

     

    I'm wanting to bind the "Top" property to a value returned by a converter...

    Wednesday, September 5, 2007 2:40 PM

Answers

  •  

    You can't just bind one of the properties of the Margin property because it is of type Thickness which is not a DependencyObject subclass and therefore doesn't support binding's directly. You would need to bind the whole Thickness value to the Rectangle and your converter implementation could take a Thickness parameter which it only changes the Top value of when being evaluated on the Binding. That might look something like this:

     

    Code Snippet

    <Rectangle ...>

    <Rectangle.Margin>

    <Binding Source="..." Path="... "Converter="{StaticResource MyMarginConverter}">

    <Binding.ConverterParameter>

    <Thickness Left="0" Top="5" Right="14" Bottom="7" >

    </Binding.ConverterParameter>

    </Binding>

    </Rectangle.Margin>

    </Rectangle>

     

     

    MyMarginConverter would be declared in a ResourceDictionary somewhere like Application.Resources and would take the parameter it's passed (expecting Thickness instances) and simply take the bound value and change the Top property of the Thickness.

     

    HTH,
    Drew

    Wednesday, September 5, 2007 4:24 PM
    Moderator