Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#WPF在运行时调整控件大小_C#_Wpf - Fatal编程技术网

C#WPF在运行时调整控件大小

C#WPF在运行时调整控件大小,c#,wpf,C#,Wpf,我在画布中找到了一个调整控件大小的方法,没有“装饰器” 我试图在我的代码中使用它,但有相当多的差异 我的画布中有多个内容 自由空间用于移动窗口 事件由我的控件触发 控件使用这部分代码动态创建: public void createNewModuleContainer() { //Parts Grid moduleGrid = new Grid(); UserControl ucModule = new Us

我在
画布
中找到了一个调整
控件
大小的方法,没有“装饰器”

我试图在我的代码中使用它,但有相当多的差异

  • 我的
    画布中有多个
    内容
  • 自由空间用于移动
    窗口
  • 事件由我的
    控件触发
  • 控件
    使用这部分代码动态创建:

         public void createNewModuleContainer()
        {
            //Parts
            Grid moduleGrid = new Grid();            
            UserControl ucModule = new UserControl();
            Grid editGrid = new Grid();
            Rectangle whiteBody = new Rectangle();
            ComboBox cbModules = new ComboBox();
            Button btnDeleteModule = new Button();
            //Hierarchie
            modulesBody.Children.Add(moduleGrid);            
            moduleGrid.Children.Add(ucModule);
            moduleGrid.Children.Add(editGrid);
            editGrid.Children.Add(whiteBody);
            editGrid.Children.Add(cbModules);
            editGrid.Children.Add(btnDeleteModule);
    
            //moduleGrid
            moduleGrid.Width = 100;
            moduleGrid.Height = 50;
            Canvas.SetLeft(moduleGrid, 100);
            Canvas.SetTop(moduleGrid, 100);
    
            //ucModule
            ucModule.HorizontalAlignment = HorizontalAlignment.Stretch;
            ucModule.VerticalAlignment = VerticalAlignment.Stretch;
            ucModulList.Add(ucModule);
    
            //editgrid
            editGrid.HorizontalAlignment = HorizontalAlignment.Stretch;
            editGrid.VerticalAlignment = VerticalAlignment.Stretch;
            editGrid.Margin = new Thickness(0, 0, 0, 0);
            editGrid.Background = new SolidColorBrush(Colors.Transparent);
            editGrid.MouseLeftButtonDown += moduleGrid_MouseDown;
            editGrid.MouseLeftButtonUp += moduleGrid_MouseUp;
            editGrid.MouseMove += moduleGrid_MouseMove;
    
            //whitebody
            whiteBody.Fill = new SolidColorBrush(Colors.White);
            whiteBody.Opacity = 0.5;            
            whiteBody.HorizontalAlignment = HorizontalAlignment.Stretch;
            whiteBody.VerticalAlignment = VerticalAlignment.Stretch;
    
    
            //cbModules
            cbModules.Cursor = Cursors.Hand;
            cbModules.Width = 20;
            cbModules.HorizontalAlignment = HorizontalAlignment.Right;
            cbModules.VerticalAlignment = VerticalAlignment.Top;
            cbModules.Height = 20;
            cbModules.Margin = new Thickness(0, 0, 0, 0);
            cbModules.SelectionChanged += CbModule1_SelectionChanged;
            fillCheckboxes(cbModules);
    
            //btnDelete
            btnDeleteModule.Cursor = Cursors.Hand;
            btnDeleteModule.Width = 20;
            btnDeleteModule.Height = 20;
            btnDeleteModule.HorizontalAlignment = HorizontalAlignment.Left;
            btnDeleteModule.VerticalAlignment = VerticalAlignment.Top;
            btnDeleteModule.Margin = new Thickness(0, 0, 0, 0);
            BitmapImage image = new BitmapImage(new Uri("pack://application:,,,/Timestamp;component/View/UserControls/Images/close.png"));
            btnDeleteModule.Background = new ImageBrush(image);
            btnDeleteModule.Visibility = Visibility.Visible;
            btnDeleteModule.Click += DeleteModuleButtonClicked;
    
    
        }
    
    看起来是这样的:

    我对本文中的代码进行了如下编辑:

    // The part of the grid the mouse is over.
        private enum HitType
        {
            None, Body, UL, UR, LR, LL, L, R, T, B
        };
    
        // True if a drag is in progress.
        private bool DragInProgress = false;
    
        // The drag's last point.
        private Point LastPoint;
    
        // The part of the grid under the mouse.
        HitType MouseHitType = HitType.None;
    
        // Return a HitType value to indicate what is at the point.
        private HitType SetHitType(Grid moduleGrid, Point point)
        {
           ;
            double left = Canvas.GetLeft(moduleGrid);
            double top = Canvas.GetTop(moduleGrid);
            double right = left + moduleGrid.ActualWidth;
            double bottom = top + moduleGrid.ActualHeight;
            if (point.X < left) return HitType.None;
            if (point.X > right) return HitType.None;
            if (point.Y < top) return HitType.None;
            if (point.Y > bottom) return HitType.None;
    
            const double GAP = 3;
            if (point.X - left < GAP)
            {
                // Left edge.
                if (point.Y - top < GAP) return HitType.UL;
                if (bottom - point.Y < GAP) return HitType.LL;
                return HitType.L;
            }
            if (right - point.X < GAP)
            {
                // Right edge.
                if (point.Y - top < GAP) return HitType.UR;
                if (bottom - point.Y < GAP) return HitType.LR;
                return HitType.R;
            }
            if (point.Y - top < GAP) return HitType.T;
            if (bottom - point.Y < GAP) return HitType.B;
            return HitType.Body;
        }
    
    
    
        private void SetMouseCursor()
        {
            // See what cursor we should display.
            Cursor desired_cursor = Cursors.Arrow;
            switch (MouseHitType)
            {
                case HitType.None:
                    desired_cursor = Cursors.Arrow;
                    break;
                case HitType.Body:
                    desired_cursor = Cursors.SizeAll;
                    break;
                case HitType.UL:
                case HitType.LR:
                    desired_cursor = Cursors.SizeNWSE;
                    break;
                case HitType.LL:
                case HitType.UR:
                    desired_cursor = Cursors.SizeNESW;
                    break;
                case HitType.T:
                case HitType.B:
                    desired_cursor = Cursors.SizeNS;
                    break;
                case HitType.L:
                case HitType.R:
                    desired_cursor = Cursors.SizeWE;
                    break;
            }
    
            // Display the desired cursor.
            if (Cursor != desired_cursor) Cursor = desired_cursor;
        }
        // Start dragging.
        private void moduleGrid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (!DragInProgress)
            {
                Console.WriteLine("Dragging Module");
                var element = sender as Grid;
                Grid parentGrid = element.Parent as Grid;
                selectedGrid = parentGrid;
    
                MouseHitType = SetHitType(selectedGrid, Mouse.GetPosition(modulesBody));
                SetMouseCursor();
                if (MouseHitType == HitType.None) return;
    
                LastPoint = Mouse.GetPosition(modulesBody);
                DragInProgress = true;
            }
            else
            {
                DragInProgress = false;
            }
    
    
        }
    
        // If a drag is in progress, continue the drag.
        // Otherwise display the correct cursor.
        private void moduleGrid_MouseMove(object sender, MouseEventArgs e)
        {
    
    
            var element = sender as Grid;
            Grid parentGrid = element.Parent as Grid;
    
    
    
            if (!DragInProgress)
            {
                MouseHitType = SetHitType(parentGrid, Mouse.GetPosition(modulesBody));
                SetMouseCursor();
            }
            else
            {
                // See how much the mouse has moved.
                Point point = Mouse.GetPosition(modulesBody);
                double offset_x = point.X - LastPoint.X;
                double offset_y = point.Y - LastPoint.Y;
    
                // Get the grid's current position.
                double new_x = Canvas.GetLeft(selectedGrid);
                double new_y = Canvas.GetTop(selectedGrid);
                double new_width = selectedGrid.Width;
                double new_height = selectedGrid.Height;
    
                // Update the grid.
                switch (MouseHitType)
                {
                    case HitType.Body:
                        new_x += offset_x;
                        new_y += offset_y;
                        break;
                    case HitType.UL:
                        new_x += offset_x;
                        new_y += offset_y;
                        new_width -= offset_x;
                        new_height -= offset_y;
                        break;
                    case HitType.UR:
                        new_y += offset_y;
                        new_width += offset_x;
                        new_height -= offset_y;
                        break;
                    case HitType.LR:
                        new_width += offset_x;
                        new_height += offset_y;
                        break;
                    case HitType.LL:
                        new_x += offset_x;
                        new_width -= offset_x;
                        new_height += offset_y;
                        break;
                    case HitType.L:
                        new_x += offset_x;
                        new_width -= offset_x;
                        break;
                    case HitType.R:
                        new_width += offset_x;
                        break;
                    case HitType.B:
                        new_height += offset_y;
                        break;
                    case HitType.T:
                        new_y += offset_y;
                        new_height -= offset_y;
                        break;
                }
    
                // Don't use negative width or height.
                if ((new_width > 0) && (new_height > 0))
                {
                    // Update the Grid
                    Canvas.SetLeft(selectedGrid, new_x);
                    Canvas.SetTop(selectedGrid, new_y);
                    selectedGrid.Width = new_width;
                    selectedGrid.Height = new_height;
    
                    // Save the mouse's new location.
                    LastPoint = point;
                }
            }
    
    
    
        }
    
        // Stop dragging.
        private void moduleGrid_MouseUp(object sender, MouseButtonEventArgs e)
        {
            DragInProgress = false;
        }
    
    //鼠标所在的网格部分。
    私有枚举类型
    {
    无,主体,UL,UR,LR,LL,L,R,T,B
    };
    //如果正在进行拖动,则为True。
    private bool DragInProgress=false;
    //阻力的最后一点。
    私人点;
    //鼠标下的网格部分。
    HitType MouseHitType=HitType.None;
    //返回一个HitType值以指示该点的内容。
    私有HitType SetHitType(网格模块ID,点)
    {
    ;
    double left=Canvas.GetLeft(moduleGrid);
    double-top=Canvas.GetTop(moduleGrid);
    双右=左+模块ID.ActualWidth;
    双底=顶部+模块ID.实际高度;
    如果(点X<左)返回HitType.None;
    如果(point.X>right)返回HitType.None;
    如果(点Y<顶部)返回HitType.None;
    如果(点Y>底部)返回HitType.None;
    常数双间隙=3;
    如果(X点-左侧<间隙)
    {
    //左边缘。
    如果(点Y-顶部<间隙)返回HitType.UL;
    if(bottom-point.Y