2012-03-17 44 views
0

我下面的教程和定義(在XAML文件)的Ellipse對象位於屏幕的中心,並且其中包含一個Ellipse.RenderTransform節點內的TranslateTransform節點如下:如何累計拖動Ellipse對象?

<Ellipse 
    x:Name="theEllipse" 
    Fill="White" 
    Width="200" 
    Height="200"> 
    <Ellipse.RenderTransform> 
    <TranslateTransform x:Name="theMover" /> 
    </Ellipse.RenderTransform> 
</Ellipse> 

在後面的代碼

public MainPage() 
{ 
// other stuff 
theEllipse.ManipulationDelta 
    += new EventHandler<ManipulationDeltaEventArgs>(theEllipse_ManipulationDelta); 
} 

void theEllipse_ManipulationDelta(object sender, ManipulationDeltaEventArgs e) 
{ 
theMover.X = e.CumulativeManipulation.Translation.X; 
theMover.Y = e.CumulativeManipulation.Translation.Y; 
} 

因此我可以在Ellipse向下按壓,並從它的起始位置拖動它:我下面增加一個ManipulationDelta事件處理程序Ellipse。然而,我發現,當我釋放Ellipse並再次按下它時,Ellipse跳躍並開始從其初始位置拖動而不是其當前位置。爲什麼是這樣?而且,如何將我的拖動操作定義爲累積性的,即當我再次拖動橢圓時,它包含來自哪裏的拖動操作?

回答

0

不知道,如果你有這個固定的或沒有,但這裏是它的解決方案:

添加事件處理程序manipulationStarting,並設置manipulationContainer作爲其母親。

<Window x:Class="TempProject.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="768" Width="640" 
    ManipulationStarting="window_ManipulationStarting" 
     ManipulationDelta="window_ManipulationDelta" 
    > 
<Grid x:Name="canvas"> 
    <Ellipse 
x:Name="theEllipse" 
Fill="Black" 
Width="200" 
Height="200" 
IsManipulationEnabled="True"> 
<Ellipse.RenderTransform> 
      <TranslateTransform x:Name="theMover" /> 
     </Ellipse.RenderTransform> 
    </Ellipse> 
</Grid> 
</Window> 

的功能應該是這樣的:

private void window_ManipulationDelta(object sender, ManipulationDeltaEventArgs e) 
    { 
     theMover.X = e.CumulativeManipulation.Translation.X; 
     theMover.Y = e.CumulativeManipulation.Translation.Y; 
     e.Handled = true; 
    } 

    private void window_ManipulationStarting(object sender, ManipulationStartingEventArgs e) 
    { 
     e.ManipulationContainer = canvas; 
     e.Handled = true; 
    } 

其中「帆布」是包含橢圓我的網格佈局的名稱。