- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在开发一个 UserControl,它由一个带有标题和项目列表的 block 组成(如 ItemsControl
)。用户控件动态添加到 Canvas 。我需要在呈现之前获取控件的实际大小(包括 ItemsControl
占用的空间)。我尝试重写 UserControl 的 MeasureOverride
方法,希望大小会反射(reflect)在 DesiredSize
属性中。但它不起作用。
XAML 是:
<UserControl x:Class="MyTools.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid x:Name="LayoutRoot" Background="White">
<Border Name="MainBorder" CornerRadius="5" BorderThickness="2" BorderBrush="Black">
<Grid Name="grid1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
<Grid.RowDefinitions>
<RowDefinition Height="34" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Name="titleGrid" Grid.Row="0" Background="#FF727272">
<TextBlock Name="titleText" HorizontalAlignment="Center" Text="{Binding ControlName}" VerticalAlignment="Center" FontSize="13" FontWeight="Bold" Foreground="Beige" />
</Grid>
<Grid Name="gridpr" Grid.Row="1" Background="#12C48F35">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Border Name="borderPr" CornerRadius="3" Margin="10" BorderThickness="1" BorderBrush="LightGray" Grid.Row="0">
<Grid Name="gridPr" Background="#FFC1C1C1" MouseLeftButtonUp="gridPr_MouseLeftButtonUp">
<StackPanel>
<TextBlock HorizontalAlignment="Center" Name="txtPr" Text="SubItems" VerticalAlignment="Center" Foreground="#FF584848" FontSize="12" />
<ItemsControl x:Name="pitems" ItemsSource="{Binding MyItems}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="15,0,0,0">
<TextBlock Text="{Binding MyVal}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
</Border>
</Grid>
</Grid>
</Border>
</Grid>
</UserControl>
我正在覆盖 UserControl 的 MeasureOverride,如下所示:
namespace MyTools
{
public partial class MyControl : UserControl
{
public MyControl()
{
InitializeComponent();
}
public string ControlName { get; set; }
public object MyItems { get; set; }
public class Row
{
public string MyVal { get; set; }
}
protected override Size MeasureOverride(Size availableSize)
{
var desiredSize = base.MeasureOverride(availableSize);
var sideLength = Math.Min(desiredSize.Width, desiredSize.Height);
desiredSize.Width = sideLength;
desiredSize.Height = sideLength;
return desiredSize;
}
}
}
客户端代码:
MyControl control1 = new MyControl();
control1.ControlName = "Test Name";
var test = new List<MyControl.Row>(
new MyControl.Row[]
{
new MyControl.Row {MyVal = "Item1"},
new MyControl.Row {MyVal = "Item2"},
new MyControl.Row {MyVal = "Item3"}
});
control1.MyItems = test;
control1.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
MessageBox.Show(control1.DesiredSize.Height.ToString());
canvas1.Children.Add(control1);
我没有使用客户端的 DesiredSize.Height
属性获取实际大小。知道如何解决这个问题吗?
最佳答案
您的用户控件是否定义为改变高度以反射(reflect)内容的大小?默认情况下,您的用户控件将具有固定大小,不会因为项目控件具有更多条目而改变高度。
我认为您需要在测量网格之前将您的用户控件添加到网格中。这就是我测量控件的方式,它似乎运行良好,即使直接测量控件在您的情况下不起作用......
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
关于c# - 如何覆盖 MeasureOverride 以查找 ItemsControl 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8844994/
我有一个来自 WrapPanel 的派生类,它覆盖了 MeasureOverride 方法。我正在构建我的应用程序的骨架/原型(prototype),首先进行 View 模型和 View 以获得屏幕流
我有一个“托管”在自定义面板(都是我的面板)中的自定义控件。 我的自定义面板有这样的代码: protected override Size MeasureOverride(Size available
如果父控件询问其子控件“你想要多大?”,那么传递的 availableSize 参数有什么用?我已经通过 Reflector 查看了 StackPanel 的源代码,但我仍然无法理解。 如果child
我这里的问题更多是理论性的,而不是功能性的。所以我正在寻找的是一个可以用一些文档来支持的答案。 我发现自己处于需要为 wpf 控件的子控件做一些自定义测量/安排的情况? 是否可以根据其他大小限制而不是
有人可以为这个功能提供一个简单的例子吗?我想要实现的是这样的: 当我有 1 个 child 时 - 我想全屏显示; 当我有 2 个 child 时 - 我希望具有相同的高度和宽度,并且每个 child
我正在开发一个 UserControl,它由一个带有标题和项目列表的 block 组成(如 ItemsControl)。用户控件动态添加到 Canvas 。我需要在呈现之前获取控件的实际大小(包括 I
我有一个如下所示的用户控件: 除了最小/最大尺寸限制之外,我还希望控件始终使用Width = Height进行绘制。所以我覆盖 MeasureOverride
如果 VerticalScrollbarVisibility的 Scrollviewer设置为自动,MeasureOverride方法ScrollViewer ,首先设置ComputedVertica
当 ListView 被分组并且 ScrollViewer.CanContentScroll="True" Sc
我正在尝试构建一个包含一个 child 的简单面板,并将其对齐到最接近的网格大小。我很难通过覆盖MeasureOverride和ArrangeOverride来弄清楚如何做到这一点。 这就是我所追求的
我正在编写一个继承自 FrameworkElement 的自定义控件。我使用 DrawingVisual 渲染到屏幕上。因此,我必须自己计算我绘制的 Visuals 的最佳尺寸。如果 Horizo
我从处理 WPF 中的 Panel 子类化和实现他们自己的布局工作以及为传递给 availableSize arg 中的 MeasureOverride 的维度获取 Infinity 或 Positi
我一直在努力确定为什么我们的 WPF 应用程序在生产环境中的运行速度比在本地机器上慢大约 3 倍。我一直在使用我们在生产中使用的一个 Blade 来测试对设置的各种更改,以尝试获得相同的时间。我们最终
我是一名优秀的程序员,十分优秀!