的頂部顯示藍色邊框
似乎像ScrollView的孩子不吸引視野外?
這是因爲Grid
內容的大小有限這也正是本身的大小,而是ScrollViewer
的內容沒有限制大小,因爲它是滾動能,不喜歡Grid
,的子元素一個ScrollViewer
將始終在其內部呈現。
...,我想對電網的頂部顯示藍色邊框
要坦率地說,我認爲這是不使用Canvas.ZIndex
實現拖放正確的方向不同父母之間的姿態。 Canvas.ZIndex值由設置值的最直接的父Canvas元素解釋。該值用於在子元素重疊的情況下顯式定義繪製順序。在這種情況下,您的Grid
和ScrollViewer
具有相同的父代,但Canvas
有不同的父代。
在這裏我可以給你一個方法來實現這個功能:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" x:Name="rootGrid"
PointerPressed="rootGrid_PointerPressed"
PointerReleased="rootGrid_PointerReleased">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="200"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Canvas x:Name="canvas" Grid.Column="1" Background="Wheat">
<Border BorderThickness="2" BorderBrush="Red" Width="50" Height="50" />
<Ellipse Width="50" Height="50" VerticalAlignment="Top" Fill="Red" />
</Canvas>
<ScrollViewer x:Name="scrollViewer" Grid.Column="0"
VerticalScrollMode="Enabled" VerticalScrollBarVisibility="Hidden"
Background="LightBlue">
<Canvas Width="100" x:Name="canvasInsideScrollViewer">
<Border BorderThickness="2" BorderBrush="Blue" Width="50" Height="50" VerticalAlignment="Bottom" />
</Canvas>
</ScrollViewer>
</Grid>
後面的代碼:
public Page20()
{
this.InitializeComponent();
this.Loaded += Page20_Loaded;
}
private void Page20_Loaded(object sender, RoutedEventArgs e)
{
gridRect = canvas.TransformToVisual(rootGrid).TransformBounds(new Rect(0.0, 0.0, canvas.ActualWidth, canvas.ActualHeight));
scrollViewerRect = scrollViewer.TransformToVisual(rootGrid).TransformBounds(new Rect(0.0, 0.0, scrollViewer.ActualWidth, scrollViewer.ActualHeight));
}
private Rect gridRect;
private Rect scrollViewerRect;
private FrameworkElement MoveElement;
private void rootGrid_PointerPressed(object sender, PointerRoutedEventArgs e)
{
var pointer = e.GetCurrentPoint(rootGrid);
if (gridRect.Left <= pointer.Position.X && pointer.Position.X <= gridRect.Right &&
gridRect.Top <= pointer.Position.Y && pointer.Position.Y <= gridRect.Bottom)
{
foreach (var childElement in canvas.Children)
{
var element = childElement as FrameworkElement;
Rect childBounds = element.TransformToVisual(rootGrid).TransformBounds(new Rect(0.0, 0.0, element.ActualWidth, element.ActualHeight));
if (childBounds.Left <= pointer.Position.X && pointer.Position.X <= childBounds.Right &&
childBounds.Top <= pointer.Position.Y && pointer.Position.Y <= childBounds.Bottom)
{
MoveElement = element;
canvas.Children.Remove(element);
}
}
}
else if (scrollViewerRect.Left <= pointer.Position.X && pointer.Position.X <= scrollViewerRect.Right &&
scrollViewerRect.Top <= pointer.Position.Y && pointer.Position.Y <= scrollViewerRect.Bottom)
{
foreach (var childElement in canvasInsideScrollViewer.Children)
{
var element = childElement as FrameworkElement;
Rect childBounds = element.TransformToVisual(rootGrid).TransformBounds(new Rect(0.0, 0.0, element.ActualWidth, element.ActualHeight));
if (childBounds.Left <= pointer.Position.X && pointer.Position.X <= childBounds.Right &&
childBounds.Top <= pointer.Position.Y && pointer.Position.Y <= childBounds.Bottom)
{
MoveElement = element;
canvasInsideScrollViewer.Children.Remove(element);
}
}
}
}
private void rootGrid_PointerReleased(object sender, PointerRoutedEventArgs e)
{
var pointer = e.GetCurrentPoint(rootGrid);
if (MoveElement != null)
{
if (gridRect.Left <= pointer.Position.X && pointer.Position.X <= gridRect.Right &&
gridRect.Top <= pointer.Position.Y && pointer.Position.Y <= gridRect.Bottom)
{
var canvasPointer = e.GetCurrentPoint(canvas);
canvas.Children.Add(MoveElement);
Canvas.SetLeft(MoveElement, canvasPointer.Position.X);
Canvas.SetTop(MoveElement, canvasPointer.Position.Y);
}
else if (scrollViewerRect.Left <= pointer.Position.X && pointer.Position.X <= scrollViewerRect.Right &&
scrollViewerRect.Top <= pointer.Position.Y && pointer.Position.Y <= scrollViewerRect.Bottom)
{
var scrollviewPointer = e.GetCurrentPoint(canvasInsideScrollViewer);
canvasInsideScrollViewer.Children.Add(MoveElement);
Canvas.SetLeft(MoveElement, scrollviewPointer.Position.X);
Canvas.SetTop(MoveElement, scrollviewPointer.Position.Y);
}
}
MoveElement = null;
}
正如你所看到的,我在第二列改Grid
到Canvas
,使該元素可以作爲鼠標點呈現在絕對位置。這裏是我的演示的渲染圖像:
這裏最令人困惑的是,你要翻譯不同的父控件之間UIElement
,但如果你把你的Border
S(要拖放)也rootGrid
,你的Grid
,,Border
s會有相同的父母,那麼你可以按照CustomBehaviorControl的XAMLBehaviorsSample來完成拖放工作。
好的,謝謝澄清z-index。我意識到這可能不是實現我想要的拖放功能的最佳解決方案。從我的問題中可能不清楚的是,我希望元素在拖動時可見,這是問題出現的地方。 –
@JonatanBlom,但它可以被拖放到相同的父控件中嗎?它僅僅從渲染的佈局上不會有什麼不同。 –