WPF using ResizeGrip to resize controls

前端 未结 1 404
醉梦人生
醉梦人生 2020-12-29 13:42

I want that the user can resize a control by draging a resize-grip on the lower right border. With the ResizeGrip there seems to exists the perfect control for

1条回答
  •  梦谈多话
    2020-12-29 14:01

    Ok, I got bored last night and wrote a little sample for you using Thumb. You should be able to Copy/Paste/Compile/Run.

    But basically, I created a UserControl named DialogReplica, just something that looks like a dialog with a grip, you can see it thrown in the main window.

    
    
        
    
    

    Here's the xaml for the UserControl (you mostly interested in the Thumb part):

    
    
    
        
    
            
                
                    
                    
                
            
    
            
    
                
                    
                        
                                                
                                            
                                        
                                    
                                
                            
                        
                    
                
    
                
                    

    finally, here's the code behind for the UserControl

    public partial class DialogReplica : UserControl
    {
        private Cursor _cursor;
    
        public DialogReplica()
        {
            InitializeComponent();
        }
    
        private void OnResizeThumbDragStarted(object sender, DragStartedEventArgs e)
        {
            _cursor = Cursor;
            Cursor = Cursors.SizeNWSE;
        }
    
        private void OnResizeThumbDragCompleted(object sender, DragCompletedEventArgs e)
        {
            Cursor = _cursor;
        }
    
        private void OnResizeThumbDragDelta(object sender, DragDeltaEventArgs e)
        {
            double yAdjust = sizableContent.Height + e.VerticalChange;
            double xAdjust = sizableContent.Width + e.HorizontalChange;
    
            //make sure not to resize to negative width or heigth            
            xAdjust = (sizableContent.ActualWidth + xAdjust) > sizableContent.MinWidth ? xAdjust : sizableContent.MinWidth;
            yAdjust = (sizableContent.ActualHeight + yAdjust) > sizableContent.MinHeight ? yAdjust : sizableContent.MinHeight;
    
            sizableContent.Width = xAdjust;
            sizableContent.Height = yAdjust;
        }
    }
    

    0 讨论(0)
提交回复
热议问题