简体   繁体   English

如何使用常用方法拖放控件?

[英]How can I drag and drop controls using a common method?

I have the following XAML code, which creates two stack panels within a big parent Stack Panel. 我有以下XAML代码,该代码在一个较大的父堆栈面板中创建两个堆栈面板。 I would like to be able to drag each small stack panel within the parent bigStack panel. 我希望能够在父bigStack面板中拖动每个小堆栈面板。

XAML XAML

<StackPanel BorderThickness="1" BorderBrush="Black" x:Name="bigStack">
        <StackPanel x:Name="smallStack1" ManipulationMode="All" ManipulationDelta="objectManipulationDelta" ManipulationStarting="objectManipulationStarting">
            <TextBlock Text="John Doe"/>
            <TextBlock Text="CEO"/>
        </StackPanel>
        <StackPanel x:Name="smallStack2" ManipulationMode="All" ManipulationDelta="objectManipulationDelta" ManipulationStarting="objectManipulationStarting">
            <TextBlock Text="Jane Doe"/>
            <TextBlock Text="CTO"/>
        </StackPanel>
</StackPanel>

C# backend: C#后端:

private TranslateTransform dragtranslation ;

private void objectManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        dragtranslation.X += e.Delta.Translation.X;
        dragtranslation.Y += e.Delta.Translation.Y;

    }

private void objectManipulationStarting(object sender, ManipulationStartingRoutedEventArgs e)
    {
       var stackDragged = e.OriginalSource as StackPanel;

        dragtranslation = new TranslateTransform();
        stackDragged.RenderTransform = this.dragtranslation ;


    }

Original Code found here (Official Microsoft UWP Documentation) but adapted (obviously wrongly) to suit my needs 可在此处找到原始代码(Microsoft UWP官方文档),但根据我的需要进行了改编(显然是错误的)

PROBLEM 1 问题1

1) Drag smallStack1 for the first time: OK 1)第一次拖动smallStack1:确定

2) Drag smallStack2 for the second time: Reverts back to the original position 2)第二次拖动smallStack2:恢复到原始位置

PROBLEM 2 问题2

1) Drag smallStack1 for the first time: OK 1)第一次拖动smallStack1:确定

2) Drag smallStack2 for the first time: OK 2)第一次拖动smallStack2:确定

3) Drag either of the smallStacks again: Reverts back to the original position 3)再次拖动任一smallStack:恢复到原始位置

You can check the problems in the .gif below: 您可以在以下.gif文件中检查问题:

在此处输入图片说明

WHAT I WISH TO ACCOMPLISH 我希望达成的目标

Drag the controls using a common method, because I plan to dynamically create more controls inside the bigStack panel. 使用通用方法拖动控件,因为我计划在bigStack面板中动态创建更多控件。

You are basically reinstantiating TranslateTransform everytime you click on an Item. 基本上,每次单击项目时,您都在重新实例化TranslateTransform That is the reason why when you click on the item second time, it navigates back to 0,0 which is the original position for TranslateTransform. 这就是为什么当您第二次单击该项目时,它会导航回到0,0 ,这是TranslateTransform的原始位置。

To Handle this in a easier way, this is what I would do. 为了更轻松地处理此问题,这就是我要做的。

1) I would give explicit TranslateTransform to the smallStackPanel 's 1)我会给smallStackPanel明确的TranslateTransform

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel BorderThickness="1" BorderBrush="Black" x:Name="bigStack">
        <StackPanel x:Name="smallStack1" ManipulationMode="All" ManipulationDelta="objectManipulationDelta" >
            <StackPanel.RenderTransform>
                <TranslateTransform />
            </StackPanel.RenderTransform>
            <TextBlock Text="John Doe"/>
            <TextBlock Text="CEO"/>
        </StackPanel>
        <StackPanel x:Name="smallStack2" ManipulationMode="All" ManipulationDelta="objectManipulationDelta" >
            <StackPanel.RenderTransform>
                <TranslateTransform />
            </StackPanel.RenderTransform>
            <TextBlock Text="Jane Doe"/>
            <TextBlock Text="CTO"/>
        </StackPanel>
    </StackPanel>
</Grid>

And then all i need to do in codebehind is handle ManipulationDelta . 然后,我在代码隐藏中所需要做的就是处理ManipulationDelta

private void objectManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    var stackDragged = e.OriginalSource as StackPanel;
    (stackDragged.RenderTransform as TranslateTransform).X += e.Delta.Translation.X;
    (stackDragged.RenderTransform as TranslateTransform).Y += e.Delta.Translation.Y;
}

Output: 输出:

在此处输入图片说明

Update 更新

To add TranslateTransform from Code 从代码添加TranslateTransform

StackPanel sp = new StackPanel();
sp.RenderTransform = new TranslateTransform();

Good Luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM