2012-11-07 14 views
5

Sto sviluppando un'applicazione di disegno in cui è possibile disegnare vari tipi di linee. Sulla linea punti d'angolo ho messo un pollice usando un itemsControl. I pollici dovrebbero spostare quel punto d'angolo quando l'utente lascia il mouse su di esso e trascina il mouse. Quello che sta accadendo ora è che quando faccio questo il punto e il pollice si muovono un po ', ma poi perde subito l'acquisizione del mouse e non si muove più. Quando eseguo il debug del primo evento dragdelta che viene generato correttamente, un albero visivo completo viene tracciato dal pollice di invio a itemscontrol e oltre, ma a volte quando si attiva la volta successiva, la posizione del pollice non è stata aggiornata e il content presenter contenuto ha genitori nulli nell'albero visivo.I punti d'angolo di un pollice su linea non riposizionano i punti

Questa è la porzione del xaml relative ai punti linea pollice:

<ItemsControl x:Name="PART_LineRelocate" ItemsSource="{Binding pointsObservableCollection}" Visibility="Collapsed" > 
    <ItemsControl.ItemTemplate> 
     <DataTemplate DataType="{x:Type s:LineCornerPoint}" > 
      <Grid> 
       <c:PointRelocateThumb VerticalAlignment="Center" HorizontalAlignment="Center" Focusable="True" x:Name="PART_PointRelocateThumb" Cursor="Hand"/> 
      </Grid> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Canvas IsItemsHost="True" /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemContainerStyle > 
     <Style > 
      <Style.Triggers> 
        <DataTrigger Value="BrokenLinkLine" Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type s:ToolLine}}, Path=indicator}" > 
         <Setter Property="Canvas.Left" Value="{Binding Corner.X}" /> 
         <Setter Property="Canvas.Top" Value="{Binding Corner.Y}" /> 
        </DataTrigger> 

        ....More of these datatriggers for the different types of lines 
       </Style.Triggers> 
     </Style> 
    </ItemsControl.ItemContainerStyle> 

Il codice per PointRelocateThumb:

public PointRelocateThumb() 
    { 

     base.DragDelta += new DragDeltaEventHandler(this.PointRelocateThumb_DragDelta); 

    } 
    void PointRelocateThumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) 
    { 
     PointRelocateThumb prt = (PointRelocateThumb)sender; 
     LineCornerPoint lcp = (LineCornerPoint)prt.DataContext; 
     Point thumbPoint = new Point(Canvas.GetLeft((ContentPresenter)prt.TemplatedParent), Canvas.GetTop((ContentPresenter)prt.TemplatedParent)); 
     ToolLine toolLine = null; 
     DrawingCanvas designer = null; 
     ItemsControl itemsControl = ItemsControl.ItemsControlFromItemContainer(prt.TemplatedParent); 
     if (itemsControl != null) 
      toolLine = itemsControl.DataContext as ToolLine; 
     if (toolLine != null) 
      designer = VisualTreeHelper.GetParent(toolLine) as DrawingCanvas; 
     if (toolLine != null && designer != null && toolLine.IsSelected) 
     { 
      thumbPoint = new Point(thumbPoint.X + Canvas.GetLeft(toolLine) + 3.5, thumbPoint.Y + Canvas.GetTop(toolLine) + 3.5); 

      toolLine.undoBounding(); 
      if (System.String.Compare(toolLine.indicator, "BrokenLinkLine") == 0 
       || System.String.Compare(toolLine.indicator, "LinkLine") == 0 
       || System.String.Compare(toolLine.indicator, "OrthogonalLinkLine") == 0 
       || System.String.Compare(toolLine.indicator, "BrokenLine") == 0 
       || System.String.Compare(toolLine.indicator, "Line") == 0 
       || System.String.Compare(toolLine.indicator, "Polygon") == 0) 
      { 
       if (toolLine.pathFigure.StartPoint.Equals(thumbPoint)) 
       { 
        toolLine.pathFigure.StartPoint = new Point(modifyingToolLine.pathFigure.StartPoint.X + e.HorizontalChange, modifyingToolLine.pathFigure.StartPoint.Y + e.VerticalChange);     } 
       else 
       { 
        foreach (LineSegment ls in toolLine.pathSegmentCollection) 

        { 
         if (ls.Point.Equals(thumbPoint)) 
         { 
          ls.Point = new Point(ls.Point.X + e.HorizontalChange, ls.Point.Y + e.VerticalChange); 
          break; 
         } 

        } 
       } 
      } 
      toolLine.regenerateBoundingItem(); 
     } 
     e.Handled = true; 
    } 
} 

}

Tooline è la linea classe. Ciò che fa il binding di annullamento è che rende così che toolLine abbia Canvas.Left e Canvas.Top impostati su 0, ma ridimensiona i punti in modo che si trovino ancora nello stesso punto - cioè aggiungendo il vecchio Canvas.Left e Canvas. Valori massimi della toolline in ogni punto della linea.

Il codice per rigenerare delimitazione voce è sotto:

public void regenerateBoundingItem() 
{ 
    //Following line of code just regenerates the underlying path for the toolLine from the 
    //new pathsegmentCollection and pathFigure start point. 
    regenerateThisLine(); 
    double leftMostPoint = double.MaxValue, topMostPoint = double.MaxValue, bottomMostPoint = double.MinValue, rightMostPoint = double.MinValue; 
    getBoundingPoints(ref leftMostPoint, ref topMostPoint, ref bottomMostPoint, ref rightMostPoint); 

    //subtracts leftMost point and topMostPoint from each point in the line 
    scaleLinePoints(leftMostPoint, topMostPoint); 
    Canvas.SetLeft(this, leftMostPoint); 
    Canvas.SetTop(this, topMostPoint); 
    this.Width = rightMostPoint - leftMostPoint; 
    this.Height = bottomMostPoint-topMostPoint; 
     regenerateObservableCollection(); 
     regenerateThisLine(); 
} 
private void regenerateObservableCollection() 
{ 
    if (this.pointsObservableCollection == null) 
     this.pointsObservableCollection = new ObservableCollection<LineCornerPoint>(); 
    this.pointsObservableCollection.Clear(); 
    LineCornerPoint startPt = new LineCornerPoint(new Point(this.pathFigure.StartPoint.X - 3.5, this.pathFigure.StartPoint.Y - 3.5)); 
    this.pointsObservableCollection.Add(startPt); 
    if (System.String.Compare(indicator, "BrokenLine") == 0 
     || System.String.Compare(indicator, "BrokenLinkLine") == 0 
     || System.String.Compare(indicator, "LinkLine") == 0 
     || System.String.Compare(indicator, "Polygon") == 0 
     || System.String.Compare(indicator, "Line") == 0) 
    { 
     foreach (LineSegment ls in pathSegmentCollection) 
     { 
      LineCornerPoint pt = new LineCornerPoint(new Point(ls.Point.X - 3.5, ls.Point.Y - 3.5)); 
      this.pointsObservableCollection.Add(pt); 
     } 
    } 
} 

Il modello per PointRelocateThumb è un'ellisse di larghezza e altezza 7 - il che spiega il motivo per cui devo compensare tutte le posizioni del pollice del 3,5.

risposta

0

Il problema era con regenerateObserableCollection

Invece di compensazione e distruggendo tutte le LineCornerObjects, ho dovuto modificare le LineCornerPoints contenuti nella raccolta osservabile.

Problemi correlati