Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've done something similar before by using a MultiBinding and a converter.</p> <p>You bind the top of the textblock, as a multibinding to the Y of the first point and the Y of the second point and use a converter to average them to a single value, then repeat for the Left = average of X's.</p> <p>Something like</p> <pre><code>&lt;Canvas.Left&gt; &lt;MultiBinding Converter="{StaticResource findCentreConverter}"&gt; &lt;Binding ElementName=yourLine, Path=X1/&gt; &lt;Binding ElementName=yourLine, Path=X2/&gt; &lt;/MultiBinding&gt; &lt;/Canvas.Left&gt; </code></pre> <p>repeat for Canvas.Top</p> <p>and your FindCentreConverter is something like</p> <pre><code>public class FindCentreMultiConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var point1 = (double)values[0]; var point2 = (double)values[1]; return (double)((point1 + point2) / 2.0d); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } </code></pre> <p>Obviously this will put the text starting at the centre of the line, but you can modify the converter to take additional values or a parameter, such as an offset for the X/Y to avoid this. Have a look at <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx</a> for an example with a parameter .</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload