WPF动态拖动 [英] WPF Dynamic Dragging

查看:131
本文介绍了WPF动态拖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我最近在WPF中下载并使用了拖动画布".这符合我要对该程序执行的操作,但是我想在运行时添加UI对象并使它们可拖动.现在,只有在运行前在XAML中定义了对象的情况下,拖动才有效.我可以为之前添加的这些对象关闭/打开拖动,但是我希望也能够创建要拖动的新对象.任何建议,将不胜感激.我将使用dragcanvas程序包含Mainwindow.XAML.

这是我使用的原始dragCanvas程序,只需替换mainwindow.XAML,一切都应该很好. 拖动画布中的元素 [

Hello I recently downloaded and played with the Drag Canvas in WPF. This fits what I''m trying to do with the program, but I would like to add UI objects during run-time and make them draggable. Right now dragging only works if the objects are defined in XAML before run. I can turn off/on dragging for these objects added before, but I want to be able to create new objects to drag as well. Any suggestions would be appreciated. I''ll include my Mainwindow.XAML, utilizing the dragcanvas program.

Here is the original dragCanvas program I used, just replace the mainwindow.XAML and everything should be good. Dragging Elements in a Canvas[^]

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DragCanvas1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        int counter = 0;

        public MainWindow()
        {
            InitializeComponent();      
        }
        //New TextBox Button
      
        private void textButton_Click(object sender, RoutedEventArgs e)
        {
            counter++;
            TextBox myBox = new TextBox();
            myBox.Width = 100;
            myBox.Height = 50;
            myBox.Text = counter.ToString();
            DragCanvas.SetCanBeDragged(myBox, true);
            myCanvas.Children.Add(myBox);
            //myBox = textBox1;
        }
        //Drag Button
        private void dragButton_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text == "Dragging Off")
            {
                textBox1.Text = "Dragging On";
                DragCanvas.SetCanBeDragged(textBox1, true);
            }
            else
            {
                textBox1.Text = "Dragging Off";
                DragCanvas.SetCanBeDragged(textBox1, false);
            }
       }
    }
}

推荐答案

您尚未对所尝试的内容进行过多说明,因此为您提供帮助有些困难,但是我会尝试一下:

您将不得不在画布上添加一些内容,例如文本框(它的优点是您已经在其中拖动了文本框).在C#中定义文本框时,请添加当前文本框已经具有的事件,但要更改处理程序以使其能够处理任何文本框:
You have not stated much about what you have tried, so it is a little difficult to help you, but I will take a shot:

You will have to add something to the canvas, like a textbox(which has the advantage of you already dragging a textbox around). When you define the textbox in C#, add the events that you already have for the current textboxbut change the handler so that it will handle any textbox:
private void dragButton_Click(object sender, RoutedEventArgs e)
{
    var textBox = sender as TextBox;
    if (textBox == null) return;//Don't have textbox
    if (textBox.Text == "Dragging Off")
    {
        textBox.Text = "Dragging On";
        DragCanvas.SetCanBeDragged(textBox, true);
    }
    else
    {
        textBox.Text = "Dragging Off";
        DragCanvas.SetCanBeDragged(textBox, false);
    }
}


如果您想在此处获得完整的解决方案,则需要更具体一些,但是让我看看能否帮上忙...

如果您只想在画布上添加任意控件并使它变为可拖动状态,那将稍微复杂一些,但是让我们在您要添加的特定控件类型的假设下进行操作.只是为了使一切保持井井有条,您可能无论如何都要这样做,因此很容易遍历它们.

因此,假设您要放到画布中的所有东西都可以拖到"DragContainer"(可能是Border的子类)中.您可以使用一些WPF样式来确保它们的行为相同...
You''ll need to be a bit more specific if you want to get a complete solution here, but let me see if I can help a bit...

If you just want to add any arbitrary control to the canvas and have it become draggable, that will be slightly more complicated, but let''s operate under the assumption that you have a specific type of control you''re adding. Just to keep everything organized, you might want to do this regardless, so it''s easy to iterate through them.

So, say everything you put in the canvas that you want to be draggable is inside a "DragContainer" (A subclass of Border, maybe). You can use some WPF styles to make sure they all behave the same way...
<window.resources>
	<style targettype="local:DragContainer">
		<setter>
                   Property="dragcanvas:DragCanvas.CanBeDragged" 
                   Value="True"/>
	</setter></style>
</window.resources>


这个小块将导致该类型的每个对象(将名称空间调整为您正在使用的任何对象)在将其添加到窗口或其子级后立即具有该属性.

但是从您发布的代码来看,您似乎正在尝试使用按钮来切换拖动的开启和关闭.这就是数据绑定的用处.最简单的方法是在窗口中添加切换按钮,而不仅仅是按钮:


That little block will cause EVERY object of that type (Adjust the namespaces to whatever you''re using) to have that property as soon as it''s added to the window or its children.

But from the code you posted, it looks like you''re trying to toggle dragging on and off with a button. That''s where Data Binding comes in. The simplest way would be to add a toggle button to the window instead of just a button:

<togglebutton name="btnToggleDraggable" content="Drag On/Off" />


安装到位后,您可以修改我上面发布的样式代码,如下所示:


Once that''s in place, you can modify the style code I posted above, to look like this:

<window.resources>
	<style targettype="local:DragContainer">
		<setter>
                   Property="dragcanvas:DragCanvas.CanBeDragged" 
                   Value="{Binding IsChecked,ElementName=btnToggleDraggable,Mode=OneWay}"/>
	</setter></style>
</window.resources>


因此,当您添加UI控件时,您要做的就是确保将它们包装在DragContainer中,它可以像这样简单:


So when you''re adding UI controls, all you have to do is make sure they''re wrapped up in a DragContainer, which could be as simple as this:

public class DragContainer : Border { }


启用拖动功能后,您甚至可能会花哨并在DragContainer上添加某种指示器,例如使用ValueConverter将边框变为红色或添加阴影...一旦掌握了WPF,WPF会使这些事情变得异常容易.


You could even get fancy and add some sort of indicator on the DragContainer when dragging is enabled, such as using a ValueConverter to turn the borders red or add a drop shadow... WPF makes those things incredibly easy once you get the hang of it.


这篇关于WPF动态拖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆