如何停止重叠的控件运行时. [英] how to stop overlapping controls runtime.

查看:111
本文介绍了如何停止重叠的控件运行时.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的读者,


请帮助我.

我为按钮点击编写了代码

Dear Reader,

Hi
please help me with this.

i wrote code for button click

Thumb addthumb = new Thumb();
                 addthumb.Height = 20;
                 addthumb.Width = lenght_[i]+25;
                 addthumb.Name = "L" + i.ToString() + "P" + i.ToString();
                 addthumb.Background = System.Windows.Media.Brushes.Orange;
                 addthumb.DragDelta += new DragDeltaEventHandler(myThumb_DragDelta);
                 Canvas.SetLeft(addthumb, position_array[balls]+25);
                 Canvas.SetTop(addthumb, 20);
                 canvas_container.Children.Add(addthumb);


canvas_container是我的画布的名称.
和事件


canvas_container is name of my canvas.
and event

private void myThumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
       {
           FrameworkElement element = sender as FrameworkElement;
           Canvas.SetLeft(element, Canvas.GetLeft(element) + e.HorizontalChange);
           lbl_Heightshow.Content = element.Height;
           lbl_widthshow.Content = element.Width;
           lbl_Slub_name.Content = element.Name;
           lbl_leftposition.Content = Canvas.GetLeft(element).ToString();
           if (Canvas.GetLeft(element) == 0)
           {
               lbl_leftposition.Content = "0";
           }
           else if (Canvas.GetLeft(element) < 0)
           {
               lbl_leftposition.Content = "0";
               Canvas.SetLeft(element, 0);
           }
           else if (Canvas.GetLeft(element) + element.Width > canvas_container.ActualWidth)
           {
               lbl_leftposition.Content = (canvas_container.ActualWidth - element.Width).ToString();
               Canvas.SetLeft(element, canvas_container.ActualWidth - element.Width);
           }




现在一切都很好,直到这里.
我想要的是生成的控件不应相互重叠,就像它们没有从画布中移出一样.

请协助.




now everything is fine till here.
what i want is the generated controls should not over lap on each other like they are not moving out of canvas.

please assist.

推荐答案

我认为您允许用户移动项目.您需要做的是监视项目的移动,并且不允许将项目移动到会导致重叠的位置,方法是将项目的位置(左,上)强制为有效值.过去,我是通过监视鼠标位置来手动处理移动并手动移动项目来完成此操作的.
I presume that you are allowing the user to move the items. What you need to do is monitor the move of an item, and not allow the item to move to a location that would cause an overlapping by forcing the position (left, top) of the item to a value that would be valid. I have done this in the past by manually handling the move by monitoring the mouse position, and moving the item manually.


这篇关于如何停止重叠的控件运行时.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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