내가 겪고있는 버그를 이해하는 데 어려움이 있습니다. 연한 파랑의 색 그 지역의UIElement의 이동 중에 이상한 동작이 발생합니다. 왜?
모든 캔버스이며, 그들이 이동 가능 :
나는 다음 그림에서와 같은 UI를 가지고있다. 이것은 내가 내 문제가있는 곳이다. 왼쪽 상단의 것이 문제없이 움직일 수 있습니다. 다른 두 사람, 내가 그들을 끌고있을 때, 사라집니다. 나는 이유를 설명 할 수 없다.
이
요소의 이동에 대한 코드입니다 :// this is all inside the MouseMove event handler function
// If there is no dragged element
if (this.DraggedElement == null || !this.IsDragInProgress)
return;
/*
* Calculating the new position for the dragged element
*/
// Mouse current position
Point cursor = e.GetPosition(this);
double xMove = 0;
double yMove = 0;
// Movement detected
if (cursor != MouseClickLocation)
{
// Moving on the x-axis and y-axis
xMove = cursor.X - MouseClickLocation.X;
yMove = cursor.Y - MouseClickLocation.Y;
// Actually moving the element
if (this.ConstrainToBounds(this.DraggedElement, mainWindow))
{
TranslateTransform translate = new TranslateTransform(xMove, yMove);
this.DraggedElement.RenderTransform = translate;
}
}
ConstrainToBounds의 코드() 메소드는 내가 그것을 왼쪽 상단에 대해 완벽하게 작동합니다 (창 테두리 외부의 캔버스를 이동하는 것을 허용하지 가정된다 캔버스가 아니라 다른 사람에 대한) 및 다음과 같다 :
private Boolean ConstrainToBounds(Canvas element, UIElement container)
{
try
{
Boolean respects = true;
// Values used to reset the element position to a proper location
double xReset = 0;
double yReset = 0;
// Left x-axis constraint
if (element.TranslatePoint(new Point(), container).X <= new Point(0, 0).X)
{
respects = false;
// Get elements' current position and adjust
xReset = element.TranslatePoint(new Point(), container).X + 1;
yReset = element.TranslatePoint(new Point(), container).Y;
TranslateTransform translate = new TranslateTransform(xReset, yReset);
element.RenderTransform = translate;
}
// Right x-axis constraint
if (element.TranslatePoint(new Point(), container).X + element.RenderSize.Width >= container.RenderSize.Width)
{
respects = false;
// Get elements' current position and adjust
xReset = element.TranslatePoint(new Point(), container).X - 1;
yReset = element.TranslatePoint(new Point(), container).Y;
TranslateTransform translate = new TranslateTransform(xReset, yReset);
element.RenderTransform = translate;
}
// Top y-axis constraint
if (element.TranslatePoint(new Point(), container).Y <= new Point(0, 0).Y)
{
respects = false;
// Get elements' current position and adjust
xReset = element.TranslatePoint(new Point(), container).X;
yReset = element.TranslatePoint(new Point(), container).Y + 1;
TranslateTransform translate = new TranslateTransform(xReset, yReset);
element.RenderTransform = translate;
}
// Bottom y-axis constraint
if (element.TranslatePoint(new Point(), container).Y + element.RenderSize.Height >= container.RenderSize.Height)
{
respects = false;
// Get elements' current position and adjust
xReset = element.TranslatePoint(new Point(), container).X;
yReset = element.TranslatePoint(new Point(), container).Y - 1;
TranslateTransform translate = new TranslateTransform(xReset, yReset);
element.RenderTransform = translate;
}
return respects;
}
catch (Exception ex)
{
throw ex;
}
}
Edit_1 : MainWindow.xaml의 코드를 추가 :
<Window
Name="mainWindow"
x:Class="WPF_TestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="480" Width="555">
<Grid Name="mainGrid">
<Canvas Grid.Row="0"
Grid.Column="0"
Background="AliceBlue"
Name="LeftTop"
MouseDown="onMouseDown"
MouseMove="onMouseMove"
MouseUp="onMouseUp" >
<Ellipse Fill="Blue"
Width="100"
Height="100"/>
</Canvas>
<Canvas Grid.Row="0"
Grid.Column="2"
Background="AliceBlue"
Name="RightTop"
MouseDown="onMouseDown"
MouseMove="onMouseMove"
MouseUp="onMouseUp">
<Ellipse Fill="Blue"
Width="100"
Height="100"/>
</Canvas>
<Label Grid.Row="2"
Grid.Column="0"
Name="LeftBottom">
</Label>
<Canvas Grid.Row="2"
Grid.Column="3"
Background="AliceBlue"
Name="RightBottom"
MouseDown="onMouseDown"
MouseMove="onMouseMove"
MouseUp="onMouseUp">
<Ellipse Fill="Blue"
Width="100"
Height="100"/>
</Canvas>
<Grid.RowDefinitions>
<RowDefinition Height="200" />
<RowDefinition Height="50" />
<RowDefinition Height="200" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="250" />
</Grid.ColumnDefinitions>
</Grid>
</Window>
Edit_2 : 오른쪽 상단 캔버스를 움직이면 실제로 (위치 600,0에있는)보기 바깥으로 이동한다는 것을 발견했습니다. 현재 왜 이런 일이 일어나는지 이해하려고합니다.
그 캔버스가 무엇인지, 즉'MainWindow '에서 사용되는 패널은 무엇입니까? 'Grid' 또는'Canvas'? –
주 창에는이 모든 다른 캔버스와 공간이있는 표가 있습니다. 내 게시물을 편집하고 XAML 코드도 추가합니다. – Andrei
렌더 변환을 사용하는 대신, 단지'Margin' 속성을 업데이트 한 다음 값을 경계 내에서 클램핑하지 않는 것이 어떻습니까? 따라서 'Margin.Left'가 '0'보다 작지 않은 것으로 계산하면 '0'으로 고정하십시오. '250 - Diameter'보다 크면 '250 - Diameter'로 고정하십시오. 마찬가지로'Top'을 처리합니다. '오른쪽'과 '하단'여백을 0으로 둡니다. 편집 : 고정 수학 : P –