Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It looks like your window's Canvas contains a number of ContentControls. One of these is of interest and contains a grid which in turn contains firstly an instance of your MoveLine class, and secondly a ContentPresenter containing a Rectangle.</p> <p>So your visual tree looks roughly like this:</p> <pre><code> Window1 Canvas ... ContentControl Grid MoveLine ContentPresenter Rectangle (mRect) </code></pre> <p>You're trying to handle an event in MoveLine and modify the ContentPresenter's Rectangle, mRect. You can refer to mRect only in the context of Window1.</p> <p>The problem is that as far as WPF is concerned the MoveLine class could appear anywhere, and so naturally it has no idea what mRect might mean to any particular MoveLine instance. As it happens we know that mRect is the child Rectangle of a sibling ContentPresenter which shares a parent with an instance of MoveLine.</p> <p>If you're absolutely sure that MoveLine will only ever be used here, you could use System.Windows.Media.VisualTreeHelper's GetParent(), GetChildrenCount() and GetChild() methods. You need to go "up" one level from MoveLine, across one, and then down one. You could also go up one level and then search for descendants with the name "mName". See <a href="https://stackoverflow.com/questions/636383/wpf-ways-to-find-controls">How can I find WPF controls by name or type?</a>. This isn't a very sane approach though.</p> <p>I'd be more tempted to put the handling code onto the canvas, or at least into its ContentControls, since they're the ones being affected. I would add a RoutedEvent called, say, ThumbMoved, to MoveLine. RoutedEvents can "bubble up" to be handled by ancestral controls. You can then add a handler for this event to the ContentControl containing your Rectangle, and <em>it</em> can then use mName to adjust the rectangle. See <a href="http://msdn.microsoft.com/en-us/library/ms752288.aspx" rel="nofollow noreferrer">How to: Create a Custom Routed Event</a> and <a href="http://msdn.microsoft.com/en-us/library/ms752288.aspx" rel="nofollow noreferrer">How to: Handle a Routed Event</a>. Very roughly:</p> <pre><code>class MoveLine : Thumb { public static readonly RoutedEvent ThumbMovedEvent = EventManager.RegisterRoutedEvent("ThumbMoved", RoutingStrategy.Bubble, typeof(DragDeltaRoutedEventHandler), typeof(MoveLine)); public event DragDeltaRoutedEventHandler ThumbMoved { add { AddHandler(ThumbMovedEvent, value); } remove { RemoveHandler(ThumbMovedEvent, value); } } void RaiseThumbMoved(DragDeltaEventArgs e) { DragDeltaRoutedEventArgs newEventArgs = new DragDeltaRoutedEventArgs(ThumbMovedEvent, e); RaiseEvent(newEventArgs); } private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e) { RaiseThumbMoved(e); ... } </code></pre> <p>(Where DragDeltaRoutedEventArgs is a class derived from RoutedEventArgs which provides the same deltas as DragDeltaEventArgs.) And then in your window's XAML:</p> <pre><code> ... &lt;ContentControl Width="5" Height="400" Canvas.Top="80" Canvas.Left="350" Template="{StaticResource DesignerItemTemplateLine}" MoveLine.ThumbMoved="MoveLine_ThumbMoved"&gt; &lt;Rectangle Fill="Blue" IsHitTestVisible="False" Name="mRect"&gt; ... </code></pre> <p>And code behind:</p> <pre><code>namespace DiagramDesigner { public partial class Window1 : Window { private void MoveLine_ThumbMoved(object sender, DragDeltaRoutedEventArgs e) { mRect.Foo = "Bar"; // mRect is recognised by Window1. } ... </code></pre>
 

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