title: How do I get a ContentPresenter's parent ItemsControl?
body: <p>I have a drag behavior that I modified from answeres to this <a href="https://stackoverflow.com/questions/27865441/move-items-in-a-canvas-using-mvvm">question</a>. Almost everything works, except when I drag shapes past other shapes. Here is the XAML for my ItemsControl:</p>
  <pre><code>&lt;ItemsControl x:Name=&quot;audioDisplayItemsControl&quot; 
                        ItemsSource=&quot;{Binding Shapes}&quot;&gt;
              &lt;ItemsControl.Resources&gt;
                  &lt;DataTemplate DataType=&quot;{x:Type ct:Rectangle}&quot;&gt;
                      &lt;Rectangle Width=&quot;{Binding Width}&quot;
                                 Height=&quot;{Binding Height}&quot;
                                 Fill=&quot;Black&quot;
                                 IsManipulationEnabled=&quot;True&quot;/&gt;
                  &lt;/DataTemplate&gt;
                  &lt;DataTemplate DataType=&quot;{x:Type ct:Line}&quot;&gt;
                      &lt;!-- Points reletive to ItemContainer --&gt;
                      &lt;Line X1=&quot;0&quot;
                            Y1=&quot;0&quot;
                            X2=&quot;0&quot;
                            Y2=&quot;{Binding Length}&quot;
                            Stroke=&quot;Gray&quot;
                            StrokeThickness=&quot;2&quot;
                            StrokeDashArray=&quot;4 1&quot;/&gt;
                  &lt;/DataTemplate&gt;
                  &lt;DataTemplate DataType=&quot;{x:Type ct:LabeledLine}&quot;&gt;
                      &lt;Grid&gt;
                          &lt;!-- Points reletive to ContentPresenter--&gt;
                          &lt;Line X1=&quot;0&quot;
                                Y1=&quot;0&quot;
                                X2=&quot;0&quot;
                                Y2=&quot;{Binding Length}&quot;
                                Stroke=&quot;Red&quot;
                                StrokeThickness=&quot;2&quot;/&gt;
                          &lt;TextBlock Margin=&quot;4,1,0,0&quot; Foreground=&quot;White&quot;&gt;
                              &lt;Run Text=&quot;(&quot;/&gt;
                              &lt;Run Text=&quot;{Binding Num}&quot;/&gt;
                              &lt;Run Text=&quot;) &quot;/&gt;
                              &lt;Run Text=&quot;{Binding Title}&quot;/&gt;
                          &lt;/TextBlock&gt;
                      &lt;/Grid&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ItemsControl.Resources&gt;
              &lt;ItemsControl.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      
                      &lt;Canvas&gt;
                          &lt;Canvas.Background&gt;
                              &lt;LinearGradientBrush EndPoint=&quot;0.5,1&quot; StartPoint=&quot;0.5,0&quot;&gt;
                                      &lt;GradientStop Color=&quot;Black&quot;/&gt;
                                      &lt;GradientStop Color=&quot;Black&quot; Offset=&quot;0.30&quot;/&gt;
                                      &lt;GradientStop Color=&quot;Green&quot; Offset=&quot;0.33&quot;/&gt;
                                      &lt;GradientStop Color=&quot;Green&quot; Offset=&quot;0.67&quot;/&gt;
                                      &lt;GradientStop Color=&quot;Black&quot; Offset=&quot;0.7&quot;/&gt;
                                      &lt;GradientStop Color=&quot;Black&quot; Offset=&quot;1&quot;/&gt;
                              &lt;/LinearGradientBrush&gt;
                          &lt;/Canvas.Background&gt;
                      &lt;/Canvas&gt;
                      
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ItemsControl.ItemsPanel&gt;
              &lt;ItemsControl.ItemContainerStyle&gt;
                  &lt;Style TargetType=&quot;ContentPresenter&quot;&gt;
                      &lt;Setter Property=&quot;Canvas.Top&quot; Value=&quot;{Binding Top, Mode=TwoWay}&quot;/&gt;
                      &lt;Setter Property=&quot;Canvas.Left&quot; Value=&quot;{Binding Left, Mode=TwoWay}&quot;/&gt;
                      &lt;Setter Property=&quot;Panel.ZIndex&quot; Value=&quot;{Binding ZIndex}&quot;/&gt;
                      &lt;Setter Property=&quot;ct:DragBehavior.Drag&quot; Value=&quot;{Binding CanDrag}&quot;/&gt;  &lt;----- DragBehavior attached here
                  &lt;/Style&gt;
              &lt;/ItemsControl.ItemContainerStyle&gt;
          &lt;/ItemsControl&gt;
  </code></pre>
  <p>Here is what my <code>ItemsControl</code> looks like before I add my list of shapes to it
  <a href="https://i.stack.imgur.com/LxdOx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LxdOx.png" alt="Blank Items Control" /></a></p>
  <p>After I add the shapes it looks like this
  <a href="https://i.stack.imgur.com/ilpm2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ilpm2.png" alt="enter image description here" /></a></p>
  <p>Only the red lines (<code>ct:LabeledLine</code>) are draggable, and only along the x-axis. My problem is when I drag a red line past the one to its right as shown</p>
  <p><a href="https://i.stack.imgur.com/TlxZ0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TlxZ0.png" alt="enter image description here" /></a></p>
  <p>The red line for &quot;(0) New 0&quot; is past the red line for &quot;(1) New 1&quot; but <strong>not</strong> past the text for &quot;(1) New 1&quot;. Since (0) was added before (1), the <code>ContentPresenter</code> for (0) is behind the <code>ContentPresenter</code> for (1). Now I can no longer drag (0), unless I move (1) out of the way. This is because the drag behavior is attached to the <code>contentPresenter</code>, not the red line.</p>
  <p>From the <code>MouseLeftButtonUp</code> event, I can get the <code>ContentPresenter</code> (it's just the sender), but I need to get the parent <code>ItemsControl</code> so I can get a list of its children, sort them by their x position values and assign <code>ZIndex</code> based on their sort order. This I think should solve my problem.</p>
  <p>I just don't know how to get the parent <code>ItemsControl</code>.</p>