views:

409

answers:

1

I'm writing a WPF application and I'm trying to figure out the best binding approach but have been coming up only "hack" solutions and I'm hoping there's a better way.

I have a Border object (which derives from FrameworkElement) inside a Grid. The Border's size may change because of a child TextBlock's text changing. The Border's position may change because other things in the Grid may rearrange the cell that the Border lives in.

I want to draw a Line from the lower left corner of the screen to the exact center of the Border object. The endpoint of the line that's in the exact center of the Border must always remain in the center regardless of what the Border's size or position on the screen.

How can I accomplish this?

My idea is that binding to a dependency property seems to be the ideal way to handle this. I could use an IValueConverter on a Binding object to calculate the exact center given it's ActualWidth, ActualHeight, and TranslatePoint() to find the upper left corner. However, I can't figure out what dependency property I should bind to that will alert me when the Border changes size and/or position.

A: 

I think you want to multi-bind to all the ActualXxx properties on both the Border itself, and the Grid it is contained within:

<MultiBinding Converter="{StaticResource MyConverter}">#
    <Binding Path="ActualWidth" ElementName="_border"/>
    <Binding Path="ActualHeight" ElementName="_border"/>
    <Binding Path="ActualWidth" ElementName="_grid"/>
    <Binding Path="ActualHeight" ElementName="_grid"/>
</MultiBinding>

Your other option is to do this in code and override the OnRender() method. That method could call the base class implementation and then draw the line. You would then need to invalidate the control whenever one of the above properties changes.

HTH, Kent

Kent Boogaart