我正在開發一個繪圖應用程序,其中可以繪製各種類型的線。在線條角點上,我使用itemsControl放置了一個大拇指。當用戶在鼠標上點擊鼠標並拖動鼠標時,拇指應移動該角點。現在發生的事情是,當我這樣做時,點和拇指稍微移動一點,但是它立刻失去鼠標捕捉並且不再移動。當我調試第一個被正確觸發的dragdelta事件時,會有一個完整的可視化樹,從發送的大拇指到itemscontrol以及之後,但有時當它下次觸發時,大拇指的位置尚未更新,並且包含的內容演示者在視覺樹中有空父母。在線角點的拇指不要重新定位點
這是與線路拇指點XAML的部分:
<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>
爲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是線類。撤銷綁定所做的是,它使得toolLine將Canvas.Left和Canvas.Top設置爲0,但縮放點以使它們仍位於同一點 - 即通過添加舊的Canvas.Left和Canvas。 toolLine的最高值到行中的每個點。
用於再生包圍盒項目的代碼如下:
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);
}
}
}
爲PointRelocateThumb模板是寬度和高度7的橢圓 - 這解釋了爲什麼我具有由3.5抵消所有的拇指位置。