Measuring controls created at runtime in WPF

前端 未结 3 477
悲&欢浪女
悲&欢浪女 2021-02-04 05:29

I recognise this is a popular question but I couldn\'t find anything that answered it exactly, but I apologise if I\'ve missed something in my searches.

I\'m trying to c

相关标签:
3条回答
  • 2021-02-04 05:35

    I am pretty new to WPF, but here's my understanding.

    When you update or create controls programatically, there is a non-obvious mechanism that is also firing (for a beginner like me anyway - although I've done windows message processing before, this caught me out...). Updates and creation of controls queue associated messages onto the UI dispatch queue, where the important point is that these will get processed at some point in the future. Certain properties depend on these messages being processed e.g. ActualWidth. The messages in this case cause the control to be rendered and then the properties associated with a rendered control to be updated.

    It is not obvious when creating controls programatically that there is asynchronous message processing happening and that you have to wait for these messages to be processed before some of the properties will have been updated e.g. ActualWidth.

    If you wait for existing messages to be processed before accessing ActualWidth, then it will have been updated:

        //These operations will queue messages on dispatch queue
        Label lb = new Label();
        canvas.Children.Insert(0, lb);
    
        //Queue this operation on dispatch queue
        Dispatcher.Invoke(DispatcherPriority.Render, new Action(() =>
        {
            //Previous messages associated with creating and adding the control
            //have been processed, so now this happens after instead of before...
            double width = lb.RenderSize.Width;
            width = lb.ActualWidth;
            width = lb.Width;    
        }));
    

    Update

    In response to your comment. If you wish to add other code, you can organize your code as follows. The important bit is that the dispatcher call ensures that you wait until the controls have been rendered before your code that depends on them being rendered executes:

        Label lb = new Label();
        canvas.Children.Insert(0, lb);
    
        Dispatcher.Invoke(DispatcherPriority.Render, new Action(() =>
        {
            //Synchronize on control rendering
        }));
    
        double width = lb.RenderSize.Width;
        width = lb.ActualWidth;
        width = lb.Width;
    
        //Other code...
    
    0 讨论(0)
  • 2021-02-04 05:38

    The control won't have a size until WPF does a layout pass. I think that happens asynchronously. If you're doing this in your constructor, you can hook the Loaded event -- the layout will have happened by then, and any controls you added in the constructor will have been sized.

    However, another way is to ask the control to calculate what size it wants to be. To do that, you call Measure, and pass it a suggested size. In this case, you want to pass an infinite size (meaning the control can be as large as it likes), since that's what Canvas is going to do in the layout pass:

    lb.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
    Debug.WriteLine(lb.DesiredSize.Width);
    

    The call to Measure doesn't actually change the control's Width, RenderSize, or ActualWidth. It just tells the Label to calculate what size it wants to be, and put that value in its DesiredSize (a property whose sole purpose is to hold the result of the last Measure call).

    0 讨论(0)
  • 2021-02-04 05:40

    Solved it!

    The issue was in my XAML. At the highest level of my label's template there was a parent canvas that had no height or width field. Since this did not have to modify its size for its children it was constantly set to 0,0. By removing it and replacing the root node with a border, which must resize to fit its children, the height and width fields are updated and propagated back to my code on Measure() calls.

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