c# - 继承运行时添加的控件的行为

时间:2017-08-28 20:48:01

标签: c# wpf

我想要实现的是能够在运行时移动放置在画布上的控件。目前它正在运行,但我希望能够在运行时添加控件(例如文本块或标签),并且这些控件在设计时添加了相同的控件行为。

这是XAML代码:

<Window x:Class="Behaviour.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:i="clr 

命名空间:System.Windows.Interactivity;装配= System.Windows.Interactivity”         的xmlns:定制= “CLR-名称空间:行为”

Title="MainWindow" Height="350" Width="525">

<Grid>
        <Canvas HorizontalAlignment="Left" 
            Height="271" 
            Margin="49,28,0,0" 
            VerticalAlignment="Top" 
            Width="443"
            Background="Beige">



            <Label Content="Label" Canvas.Left="47" Canvas.Top="100">
                <i:Interaction.Behaviors>
                    <custom:DragInCanvasBehaviour>
                    </custom:DragInCanvasBehaviour>

                </i:Interaction.Behaviors>
            </Label>

            <Ellipse Fill="#FFF4F4F5" Height="100"
                 Canvas.Left="275" Stroke="Black" Canvas.Top="41" 
              Width="100">

                <i:Interaction.Behaviors>
                <custom:DragInCanvasBehaviour/>
            </i:Interaction.Behaviors>

            </Ellipse>

            <TextBlock Text="Drag meeee">
             <i:Interaction.Behaviors>
                <custom:DragInCanvasBehaviour/>
            </i:Interaction.Behaviors>
            </TextBlock>

        <Label Content="Label" 
               Canvas.Left="186" 
               Canvas.Top="18">

            <i:Interaction.Behaviors>

            <custom:DragInCanvasBehaviour>
            </custom:DragInCanvasBehaviour>
        </i:Interaction.Behaviors>
            </Label>


     </Canvas>

  </Grid>
</Window>

这是窗口背后的代码......

namespace Behaviour
{
public class DragInCanvasBehaviour : Behavior<UIElement>
{
    bool IsDragging = false;
    Point MouseOffSet;
    Canvas cnv;

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.MouseMove += AssociatedObject_MouseMove;
        AssociatedObject.MouseLeftButtonUp += 
AssociatedObject_MouseLeftButtonUp;
        AssociatedObject.MouseLeftButtonDown += 
AssociatedObject_MouseLeftButtonDown;
        AssociatedObject.MouseLeftButtonDown += (s, e) => 
System.Diagnostics.Debug.WriteLine("Clicked!");
    }
    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.MouseMove -= AssociatedObject_MouseMove;
        AssociatedObject.MouseLeftButtonUp -= 
AssociatedObject_MouseLeftButtonUp;
        AssociatedObject.MouseLeftButtonDown -= 
AssociatedObject_MouseLeftButtonDown;
    }


    protected void AssociatedObject_MouseMove(object sender, MouseEventArgs 
e)
    {
        if (IsDragging)
        {
            Point pt = e.GetPosition(cnv);
            // Move element
           // AssociatedObject.SetValue(Canvas.TopProperty, pt.Y - 
MouseOffSet.Y);
            //AssociatedObject.SetValue(Canvas.TopProperty, pt.X - 
MouseOffSet.X);
            Canvas.SetTop(AssociatedObject, pt.Y - MouseOffSet.Y);
            Canvas.SetLeft(AssociatedObject, pt.X - MouseOffSet.X); ;
        }
    }
    protected void AssociatedObject_MouseLeftButtonUp(object sender, 
MouseEventArgs e)
    {
        if (IsDragging)
        {
            AssociatedObject.ReleaseMouseCapture();
            IsDragging = false;
        }
    }
    protected void AssociatedObject_MouseLeftButtonDown(object sender, 
MouseEventArgs e)
    {
        if (cnv == null)
        {
            cnv = (Canvas)VisualTreeHelper.GetParent(AssociatedObject);
        }

        IsDragging = true;
        MouseOffSet = e.GetPosition(AssociatedObject);
        AssociatedObject.CaptureMouse();

    }

  }


}

正如我所说的,这样做很好,并且做了我之后的事情。但是id喜欢为运行时添加的每个控件执行此操作。

谢谢!

0 个答案:

没有答案