本文介绍了获取用户控件的实际尺寸绘制之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让的MyUserControl 的ActualSize之前被使用<$ C $的测量梅索德呈现C>用户控件类的建议对我。但是它不工作。 的MyUserControl 有一个的ItemsControl 一个数据绑定与列表作为如下所示。该项目通过添加 uc.MyCollection = MyCollection的; 是没有得到体现在 uc.DesiredSize.Height

  UC的MyUserControl =新的MyUserControl(); 
uc.AName =A1;
uc.Width = 194;
uc.MyCollection = MyCollection的; // MyCollection的是一个数据绑定列表的MyUserControl
uc.Measure(新尺寸(double.PositiveInfinity,double.PositiveInfinity))里面的ItemsControl;
uc.Arrange(新的Rect(0,0,uc.DesiredSize.Width,uc.DesiredSize.Width)); //需要的?
uc.UCHeight = uc.DesiredSize.Height;项目//尺寸数据绑定到ItemsControl中没有反映这里
uc.UCWidth = uc.DesiredSize.Width;
uc.Margin =新厚度(34,42,0,0);
myRoot.Children.Add(UC); // myRoot是帆布


解决方案

您必须覆盖的MeasureOverride和ArrangeOverride在你的UC来计算自己的大小。里面的MeasureOverride问:你的清单与自己规模测量 - 这样你就可以计算出大小为自己的UC(此尺寸的MeasureOverride返回) - 那么你调用uc.Measure后得到您的DesiredSize


I am trying to get the ActualSize of MyUserControl before it gets rendered using Measure methode of UserControl class as suggested for my previous question. However it is not working. MyUserControl has an ItemsControl that is databound with a List as shown below. The items added through uc.MyCollection = myCollection; is not getting reflected in uc.DesiredSize.Height.

MyUserControl uc= new MyUserControl();
uc.AName = "a1";
uc.Width = 194;
uc.MyCollection = myCollection; //myCollection is  a List databound to ItemsControl inside MyUserControl
uc.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
uc.Arrange(new Rect(0, 0, uc.DesiredSize.Width, uc.DesiredSize.Width)); //Needed?
uc.UCHeight = uc.DesiredSize.Height; //size of items databound to ItemsControl not reflected here
uc.UCWidth = uc.DesiredSize.Width;
uc.Margin = new Thickness(34, 42, 0, 0);
myRoot.Children.Add(uc); //myRoot is Canvas
解决方案

You have to override MeasureOverride and ArrangeOverride in your uc to calculate the size for your own. Inside MeasureOverride "ask" your List with "Measure" of its own size - so you can calculate the size for your own uc (returning this size in MeasureOverride) - then you get your DesiredSize after calling uc.Measure.

这篇关于获取用户控件的实际尺寸绘制之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 23:09