//辅助参考http://blog.csdn.net/zmq570235977/article/details/50392283 //参考原文通过Measure & Arrange实现UWP瀑布流布局http://www.cnblogs.com/ms-uap/p/4715195.html protected override Size MeasureOverride(Size availableSize) { // 记录每个流的长度。因为我们用选取最短的流来添加下一个元素。 KeyValuePair <double, int>[] flowLens = new KeyValuePair <double, int> [ColumnNum]; if (Orientation == Orientation.Vertical) { foreach (int idx in Enumerable.Range(0, ColumnNum)) { flowLens[idx] = new KeyValuePair <double, int>(0.0, idx); } // 我们就用2个纵向流来演示,获取每个流的宽度。 double flowWidth = availableSize.Width / ColumnNum; // 为子控件提供沿着流方向上,无限大的空间 Size elemMeasureSize = new Size(flowWidth, double.PositiveInfinity); foreach (UIElement elem in Children) { // 让子控件计算它的大小。 elem.Measure(elemMeasureSize); Size elemSize = elem.DesiredSize; double elemLen = elemSize.Height; var pair = flowLens[0]; // 子控件添加到最短的流上,并重新计算最短流。 // 因为我们为了求得流的长度,必须在计算大小这一步时就应用一次布局。但实际的布局还是会在Arrange步骤中完成。 flowLens[0] = new KeyValuePair <double, int>(pair.Key + elemLen, pair.Value); flowLens = flowLens.OrderBy(p => p.Key).ToArray(); } return(new Size(availableSize.Width, flowLens.Last().Key));//返回值是该元素本身实际需要的大小。 } else { foreach (int idx in Enumerable.Range(0, ColumnNum)) { flowLens[idx] = new KeyValuePair <double, int>(0.0, idx); } double flowHeigh = 50; Size elemMeasureSize = new Size(double.PositiveInfinity, flowHeigh); foreach (UIElement elem in Children) { elem.Measure(elemMeasureSize); Size elemSize = elem.DesiredSize; double elemLen = elemSize.Width; var pair = flowLens[0]; flowLens[0] = new KeyValuePair <double, int>(pair.Key + elemLen, pair.Value); flowLens = flowLens.OrderBy(p => p.Key).ToArray(); } return(new Size(flowLens.Last().Key, 50));//返回值是该元素本身实际需要的大小。 } }
/// <summary> /// 当在派生类中重写时,请测量子元素在布局中所需的大小,然后确定 <see cref="T:System.Windows.FrameworkElement" /> 派生类的大小。 /// 更新当前元素与其子元素的布局,以下处理都属于 测量 处理,并非实际布局 /// </summary> /// <param name="availableSize">此元素可以赋给子元素的可用大小。可以指定无穷大值,这表示元素的大小将调整为内容的可用大小。</param> /// <returns>此元素在布局过程中所需的大小,这是由此元素根据对其子元素大小的计算而确定的。</returns> protected override Size MeasureOverride(Size availableSize) { KeyValuePair <double, int>[] flowLens = new KeyValuePair <double, int> [ColumnCount]; foreach (int idx in Enumerable.Range(0, ColumnCount)) { flowLens[idx] = new KeyValuePair <double, int>(0.0, idx); } // 我们就用2个纵向流来演示,获取每个流的宽度。 double flowWidth = availableSize.Width / ColumnCount; // 为子控件提供沿着流方向上,无限大的空间 Size elemMeasureSize = new Size(flowWidth, double.PositiveInfinity); foreach (UIElement elem in Children) { // 让子控件计算它的大小。 elem.Measure(elemMeasureSize); Size elemSize = elem.DesiredSize; double elemLen = elemSize.Height; var pair = flowLens[0]; // 子控件添加到最短的流上,并重新计算最短流。 // 因为我们为了求得流的长度,必须在计算大小这一步时就应用一次布局。但实际的布局还是会在Arrange步骤中完成。 flowLens[0] = new KeyValuePair <double, int>(pair.Key + elemLen, pair.Value); flowLens = flowLens.OrderBy(p => p.Key).ToArray(); } return(new Size(availableSize.Width, flowLens.Last().Key)); }
private bool IsValid() { bool valid = true; var requiredFields = new KeyValuePair<MetroLabel, IMetroControl>[] { new KeyValuePair<MetroLabel, IMetroControl>(firstNameLabel, firstName), new KeyValuePair<MetroLabel, IMetroControl>(lastNameLabel, lastName), new KeyValuePair<MetroLabel, IMetroControl>(phoneLabel, phone), new KeyValuePair<MetroLabel, IMetroControl>(cardLabel, card) }; if (CurrentClient != null) requiredFields = new KeyValuePair<MetroLabel, IMetroControl>[] { requiredFields.Last() }; foreach (var field in requiredFields) { var label = field.Key; var textField = field.Value as MetroTextBox; label.UseCustomForeColor = false; if (textField.Text.ToString().IsNullOrWhiteSpace()) { valid = false; label.UseCustomForeColor = true; label.ForeColor = Color.Red; } label.Refresh(); } return valid; }
protected override Size MeasureOverride(Size availableSize) { //横向瀑布流 groupcount = (int)availableSize.Width / 256 >= 3 ? (int)availableSize.Width / 256 : 3; //三组流长度记录 KeyValuePair<double, int>[] flowLength = new KeyValuePair<double, int>[groupcount]; foreach (int index in Enumerable.Range(0, groupcount)) { flowLength[index] = new KeyValuePair<double, int>(0.0, index); } //每组长度为总长度1/3 double flowWidth = availableSize.Width / groupcount; //子控件宽为组宽,长无限制 Size childMeasureSize = new Size(flowWidth, double.PositiveInfinity); //子控件遍历计算长度 foreach (UIElement childElement in Children) { childElement.Measure(childMeasureSize); Size childSize = childElement.DesiredSize; //得到子控件长 double childLength = childSize.Height; //暂存最短流长度 var tempPair = flowLength[0]; //最短流长度重新计算 flowLength[0] = new KeyValuePair<double, int>(tempPair.Key + childLength, tempPair.Value); //重新按流长度排列键值对 这里以Key 的值作为排列依据,flowWidth[0]为Key最小的键值对,P可替换为任意字母 flowLength = flowLength.OrderBy(P => P.Key).ToArray(); } //返回 长:最长流的长;宽:传入的宽 return new Size(availableSize.Width, flowLength.Last().Key); }