wpf - WPF 将子控件的 ActualWidth 和 ActualHeight 传递给值转换器
问题描述
使用 WPF,我希望为集合中的每个数据集创建一个图像并显示垂直堆叠的图像。所有图像必须具有相同的高度(和宽度)。堆叠在一起的图像必须构成包含元素的高度。不得以任何方式拉伸图像。
我所追求的那种东西的一个不工作的假装例子如下。
<UniformGrid Columns="1" DataContext="{Binding DataSetCollection}">
<UniformGrid.Children>
<MultiBinding Converter="{StaticResource DataSetToImageConverter}">
<Binding />
<Binding Path="ActualWidth" RelativeSource="{RelativeSource AncestorType={x:Type UniformGridRow}}" />
<Binding Path="ActualHeight" RelativeSource="{RelativeSource AncestorType={x:Type UniformGridRow}}" />
</MultiBinding>
</UniformGrid.Children>
</UniformGrid>
由于多种原因,这不起作用,但希望能传达意图。关键要求是:
- 所有孩子的身高必须相同
- 不允许拉伸
- 每个图像都是根据可用区域动态创建的
- 当包含元素的大小发生变化时重绘图像
- 用于返回图像的值转换器正确接收包含行的宽度和高度
它不必使用 UniformGrid,它可以是任何东西,但是它应该为每个孩子使用数据绑定和转换器。
解决方案
这成功了,绑定到包含 ContentPresenter 的大小。
<ItemsControl ItemsSource="{Binding DataSetCollection}" ItemTemplate="{StaticResource WriteableBitmapDataTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="1" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
使用此数据模板
<DataTemplate x:Key="WriteableBitmapDataTemplate">
<Image>
<Image.Source>
<MultiBinding Converter="{StaticResource RangeToWritableBitmapConverter}">
<Binding />
<Binding Path="ActualWidth" RelativeSource="{RelativeSource AncestorType={x:Type ContentPresenter}}" />
<Binding Path="ActualHeight" RelativeSource="{RelativeSource AncestorType={x:Type ContentPresenter}}" />
</MultiBinding>
</Image.Source>
</Image>
</DataTemplate>
推荐阅读
- python-3.x - 如何将字符串从 DB 加载到 url_for() 烧瓶
- html - wordpress 制作一个向导来从用户那里收集信息并将其保存在数据库中
- c# - 将 C# 项目导入 Visual Studio 时的 SDK 版本问题
- javascript - 如何将只读方法附加到来自 redux 的对象?
- node.js - 限制对 AWS SQL Server 实例的外部访问
- node.js - 服务器未接收到来自客户端的 Socket.io 发出
- javascript - 在 jsconfig.json 中使用 baseUrl 不适用于 NextJS
- java - 片段内的上下文和活动有什么区别?
- c++ - OpenGL - 添加第二个着色器属性会导致空白屏幕
- visual-studio - 有没有办法在 Microsoft Visual Studio 2019 中获取 Github 选项卡