How to override MeasureOverride to find the size of ItemsControl

前端 未结 2 1311
耶瑟儿~
耶瑟儿~ 2020-12-20 21:42

I am developing a UserControl that consists of a block with a heading and a list of items (as ItemsControl). The usercontrol is added dynamically to a canvas. I

相关标签:
2条回答
  • 2020-12-20 22:10

    Is your user control defined to change in height to reflect the size of the contents? By default your user control will be a fixed size and not change in height just because the items control has more entries.

    I think you need to add your user control inside a grid before then measuring the grid. This is how I measure controls and it seems to work well, even when measuring the control directly does not work as in your case...

    MyControl control1 = new MyControl();
    
    ... your setup code for control1...
    
    Dim containerGrid As New Grid
    containerGrid.Children.Add(control1)
    containerGrid.Measure(New Size(Double.MaxValue, Double.MaxValue))
    containerGrid.Arrange(New Rect(0, 0, Double.MaxValue, Double.MaxValue))
    
    ...now check the grid.ActualWidth and grid.ActualHeight
    
    0 讨论(0)
  • 2020-12-20 22:23

    Try using ActualWidth and ActualHeight properties.

    protected override Size MeasureOverride(Size availableSize)
    {
       var desiredSize = new Size();
       var sideLength = Math.Min(ActualWidth, ActualHeight);
    
       desiredSize.Width = sideLength;
       desiredSize.Height = sideLength;
    
       return desiredSize;
    }
    
    0 讨论(0)
提交回复
热议问题