<i id='z0UbO'><tr id='z0UbO'><dt id='z0UbO'><q id='z0UbO'><span id='z0UbO'><b id='z0UbO'><form id='z0UbO'><ins id='z0UbO'></ins><ul id='z0UbO'></ul><sub id='z0UbO'></sub></form><legend id='z0UbO'></legend><bdo id='z0UbO'><pre id='z0UbO'><center id='z0UbO'></center></pre></bdo></b><th id='z0UbO'></th></span></q></dt></tr></i><div id='z0UbO'><tfoot id='z0UbO'></tfoot><dl id='z0UbO'><fieldset id='z0UbO'></fieldset></dl></div>
      <tfoot id='z0UbO'></tfoot>
      <legend id='z0UbO'><style id='z0UbO'><dir id='z0UbO'><q id='z0UbO'></q></dir></style></legend>
        <bdo id='z0UbO'></bdo><ul id='z0UbO'></ul>
    1. <small id='z0UbO'></small><noframes id='z0UbO'>

      如何拖放“盒子"?在银光下

      时间:2023-08-25

          <small id='Rbfh4'></small><noframes id='Rbfh4'>

          <tfoot id='Rbfh4'></tfoot>
            <bdo id='Rbfh4'></bdo><ul id='Rbfh4'></ul>
          • <legend id='Rbfh4'><style id='Rbfh4'><dir id='Rbfh4'><q id='Rbfh4'></q></dir></style></legend>

                  <tbody id='Rbfh4'></tbody>
                <i id='Rbfh4'><tr id='Rbfh4'><dt id='Rbfh4'><q id='Rbfh4'><span id='Rbfh4'><b id='Rbfh4'><form id='Rbfh4'><ins id='Rbfh4'></ins><ul id='Rbfh4'></ul><sub id='Rbfh4'></sub></form><legend id='Rbfh4'></legend><bdo id='Rbfh4'><pre id='Rbfh4'><center id='Rbfh4'></center></pre></bdo></b><th id='Rbfh4'></th></span></q></dt></tr></i><div id='Rbfh4'><tfoot id='Rbfh4'></tfoot><dl id='Rbfh4'><fieldset id='Rbfh4'></fieldset></dl></div>
                本文介绍了如何拖放“盒子"?在银光下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我已经创建了一个这样的盒子,现在我正在尝试使用矩形和其他对象拖放盒子,但我不知道该怎么做.

                I have a created a box like this and now i'm trying to drag and drop the box, with rectangles and other objects I did it, but with this I don't know how to do.

                这是我如何制作盒子的代码

                Here is the code of how I did the box

                XAML:

                <Canvas>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <TextBox Text="{Binding Header,UpdateSourceTrigger=PropertyChanged}"
                             BorderBrush="Black" BorderThickness="1" Canvas.Left="41" Canvas.Top="10" Width="97" />
                        <TextBox Text="{Binding Text,UpdateSourceTrigger=PropertyChanged}"
                             TextWrapping="Wrap"
                             VerticalScrollBarVisibility="Auto"
                             AcceptsReturn="True"
                             BorderBrush="Black" BorderThickness="1" Grid.Row="1" Canvas.Left="41" Canvas.Top="39" Height="53" Width="97" />
                    </Grid>
                </Canvas>
                

                c#代码:

                public partial class MyBox : UserControl
                {
                    public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(string), typeof(MyBox),null);
                    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Content", typeof(string), typeof(MyBox),null);
                
                    public string Header
                    {
                        get { return GetValue(HeaderProperty) as string; }
                        set { SetValue(HeaderProperty, value); }
                    }
                
                    public string Text
                    {
                        get { return GetValue(TextProperty) as string; }
                        set { SetValue(TextProperty, value); }
                    }
                
                    public MyBox()
                    {
                        InitializeComponent();
                        this.DataContext = this;    
                    }
                

                这是添加另一个框的代码:

                And this is the code for adding another box:

                private void Button_Click(object sender, RoutedEventArgs e)
                {
                    panel.Children.Add(new MyBox
                    {
                        //LayoutRoot.Children.Add(new MyBox  {
                        Header = "Another box",
                        Text = "...",
                        //    BorderBrush = Brushes.Black,
                        BorderThickness = new Thickness(1),
                        Margin = new Thickness(10)
                    });
                }
                

                推荐答案

                这是一个示例,灵感来自 https://stackoverflow.com/a/1495486/145757(感谢 Corey),针对我们的用例稍作调整、简化(没有额外的布尔值)和增强(考虑边距):

                Here is a sample, inspired from https://stackoverflow.com/a/1495486/145757 (thanks Corey), slightly adapted, simplified (no additional boolean) and enhanced (take margins into account) for our use-case:

                首先我修改了盒子,让它有一个专用的拖动区域:

                First I've modified the box so that it has a dedicated drag area:

                <UserControl x:Class="WpfApplication1.MyBox"
                             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" 
                             mc:Ignorable="d" 
                             d:DesignHeight="300" d:DesignWidth="300">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <TextBlock Text="Drag me" />
                        <TextBox Text="{Binding Header,UpdateSourceTrigger=PropertyChanged}"
                                 BorderBrush="Black" BorderThickness="1" Margin="2" Grid.Row="1" />
                        <TextBox Text="{Binding Text,UpdateSourceTrigger=PropertyChanged}"
                                 TextWrapping="Wrap"
                                 VerticalScrollBarVisibility="Auto"
                                 AcceptsReturn="True"
                                 BorderBrush="Black" BorderThickness="1" Margin="2" Grid.Row="2" />
                    </Grid>
                </UserControl>
                

                MainWindow XAML 稍作修改:

                MainWindow XAML slightly modified:

                <Window x:Class="WpfApplication1.MainWindow"
                        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        Title="MainWindow" Height="350" Width="525"
                        xmlns:local="clr-namespace:WpfApplication1">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Canvas x:Name="panel">
                        </Canvas>
                        <Button Content="Add" Grid.Row="1" Grid.Column="0" Click="Button_Click" />
                    </Grid>
                </Window>
                

                并且拖放引擎在代码隐藏中:

                And the drag-and-drop engine is in the code-behind:

                using System.Windows;
                using System.Windows.Media;
                using System.Windows.Input;
                using System.Windows.Controls;
                
                namespace WpfApplication1
                {
                    public partial class MainWindow : Window
                    {
                        public MainWindow()
                        {
                            InitializeComponent();
                        }
                
                        private void Button_Click(object sender, RoutedEventArgs e)
                        {
                            MyBox box = new MyBox
                            {
                                Header = "Another box",
                                Text = "...",
                                BorderBrush = Brushes.Black,
                                BorderThickness = new Thickness(1),
                                Margin = new Thickness(10)
                            };
                
                            box.MouseLeftButtonDown += Box_MouseLeftButtonDown;
                            box.MouseLeftButtonUp += Box_MouseLeftButtonUp;
                            box.MouseMove += Box_MouseMove;
                
                            panel.Children.Add(box);
                        }
                
                        private MyBox draggedBox;
                        private Point clickPosition;
                
                        private void Box_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
                        {
                            draggedBox = sender as MyBox;
                            clickPosition = e.GetPosition(draggedBox);
                            draggedBox.CaptureMouse();
                        }
                
                        private void Box_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
                        {
                            draggedBox.ReleaseMouseCapture();
                            draggedBox = null;
                        }
                
                        private void Box_MouseMove(object sender, MouseEventArgs e)
                        {
                            if (draggedBox != null)
                            {
                                Point currentPosition = e.GetPosition(panel);
                
                                draggedBox.RenderTransform = draggedBox.RenderTransform ?? new TranslateTransform();
                
                                TranslateTransform transform = draggedBox.RenderTransform as TranslateTransform;
                
                                transform.X = currentPosition.X - clickPosition.X - draggedBox.Margin.Left;
                                transform.Y = currentPosition.Y - clickPosition.Y - draggedBox.Margin.Right;
                            }
                        }
                    }
                }
                

                这篇关于如何拖放“盒子"?在银光下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 WPF 中的网格中的单元格之间拖放自定义控件 下一篇:拖动&amp;删除动态创建的快捷方式

                相关文章

                最新文章

                <legend id='jLCrK'><style id='jLCrK'><dir id='jLCrK'><q id='jLCrK'></q></dir></style></legend>
                • <bdo id='jLCrK'></bdo><ul id='jLCrK'></ul>
                <i id='jLCrK'><tr id='jLCrK'><dt id='jLCrK'><q id='jLCrK'><span id='jLCrK'><b id='jLCrK'><form id='jLCrK'><ins id='jLCrK'></ins><ul id='jLCrK'></ul><sub id='jLCrK'></sub></form><legend id='jLCrK'></legend><bdo id='jLCrK'><pre id='jLCrK'><center id='jLCrK'></center></pre></bdo></b><th id='jLCrK'></th></span></q></dt></tr></i><div id='jLCrK'><tfoot id='jLCrK'></tfoot><dl id='jLCrK'><fieldset id='jLCrK'></fieldset></dl></div>

                    <small id='jLCrK'></small><noframes id='jLCrK'>

                    <tfoot id='jLCrK'></tfoot>