0

この記事を使用して、いくつかのruntimemovebleをPorjectで作成します。誰かがどこに問題があるか知っていますか?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 に答える