0

我使用这篇文章创建了一些可移动的在runtimePorject中一切正常,但是当我托管我的. 有人知道哪里有问题吗?controlWPFWPF controlWinForm

C#可移动部分control

using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace EvTest.Resource
{

public class MoveThumb : Thumb
{
    public MoveThumb()
    {
        DragDelta += new DragDeltaEventHandler(this.MoveThumb_DragDelta);
    }

    private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e)
    {
        Control designerItem = this.DataContext as Control;


            double left = Canvas.GetLeft(designerItem);
            double top = Canvas.GetTop(designerItem);

            Canvas.SetLeft(designerItem, left + e.HorizontalChange);
            Canvas.SetTop(designerItem, top + e.VerticalChange);

    }
}
}

Xaml控制部分:

<UserControl x:Class="EvTest.Control"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:s="clr-namespace:EvTest.Resource"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>

        <!-- MoveThumb Template -->
        <ControlTemplate x:Key="MoveThumbTemplate" TargetType="{x:Type s:MoveThumb}">
            <Rectangle Fill="Transparent"/>
        </ControlTemplate>

        <!-- ResizeDecorator Template -->


        <!-- Designer Item Template-->
        <ControlTemplate x:Key="DesignerItemTemplate" TargetType="ContentControl">
            <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                <s:MoveThumb Template="{StaticResource MoveThumbTemplate}"/>
                <ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
            </Grid>
        </ControlTemplate>

    </UserControl.Resources>

    <Canvas>
        <ContentControl Width="130"
                MinWidth="50"
                Height="130"
                MinHeight="50"
                Canvas.Top="149"
                Canvas.Left="158"
                Template="{StaticResource DesignerItemTemplate}">
            <Ellipse Fill="Red"
           IsHitTestVisible="False"/>
        </ContentControl>
        <ContentControl Width="130"
                MinWidth="50"
                Height="130"
                MinHeight="50"
                Canvas.Top="12"
                Canvas.Left="12"
                Template="{StaticResource DesignerItemTemplate}">
            <Path Fill="Blue"
        Data="M 0,5 5,0 10,5 5,10 Z"
        Stretch="Fill"
        IsHitTestVisible="False"/>
        </ContentControl>
    </Canvas>


</UserControl>
4

1 回答 1

0

我发现问题。它必须是这样的:

private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
    ContentControl designerItem = this.DataContext as ContentControl;


        double left = Canvas.GetLeft(designerItem);
        double top = Canvas.GetTop(designerItem);

        Canvas.SetLeft(designerItem, left + e.HorizontalChange);
        Canvas.SetTop(designerItem, top + e.VerticalChange);

}
于 2012-09-12T14:27:28.127 回答