Bind to margin top property? RRS feed

  • 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



    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 ...>


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


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







    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.



    Wednesday, September 5, 2007 4:24 PM