WPF XAML:如何自动缩小 XAML 中组件的子级?
这是我的问题:
xaml 文件 1:模板化列表控件
<ItemsControl ItemSource={Binding myUIrelatedDataList}/>
<ItemsControl.ItemTemplate>
<DataTemplate>
<my:DynamicUIPresenter Width="160" Height="80"/>
<!-- here, I want to specify my desired size to which i want to
scale down the generated UIElements -->
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl/>
xaml 文件 2:项目模板
<Border BorderBrush="Black" BorderThickness="1">
<Border Child="{Binding Path=., Converter={StaticResource DynaUIConverter}}"/>
</Border>
问题是我对 myUIlatedDataList
以及 DynaUIConverter< 的 UIElements 的确切类型一无所知。 /code> 正在制作。 DynaUIConverter 通常会生成一组相互嵌套并包含文本框、按钮等的 Stackpanels。这些面板没有设置宽度或高度,我可能无法修复这一点。
我认为我需要测量生成的 UI 的最小尺寸要求,并将 ScaleTransform 动态应用到生成的 UI,或者可能更好地应用到包含生成的 UI 的 Border。
我在后面的代码中尝试了这一点,但是缩放无法正常工作,因为我无法理解从孩子到父母的宽度和高度传播如何/是否真正有效。而且我还遇到了一些递归问题,这使得代码最终效率很低。
现在的问题是:我可以在 XAML 中直接/轻松地缩小子元素吗?
Here's my problem:
xaml file 1: a templated list control
<ItemsControl ItemSource={Binding myUIrelatedDataList}/>
<ItemsControl.ItemTemplate>
<DataTemplate>
<my:DynamicUIPresenter Width="160" Height="80"/>
<!-- here, I want to specify my desired size to which i want to
scale down the generated UIElements -->
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl/>
xaml file 2: the item template
<Border BorderBrush="Black" BorderThickness="1">
<Border Child="{Binding Path=., Converter={StaticResource DynaUIConverter}}"/>
</Border>
The problem is that I do not know anything about the myUIrelatedDataList
and what exact kind of UIElements the DynaUIConverter
is producing. The DynaUIConverter
usually produces a set of Stackpanels nested in each other and containing TextBoxes, Buttons, etc. These panels do not have a Width or Height set, which I might not be able to fix.
I think I need to measure the minimum size requirements of the generated UI and dynamically apply a ScaleTransform to the produced UI, or maybe better to the Border containing the generated UI.
I tried this in code behind, but the scaling did not work correctly, since I was not able to understand how/if width and height propagation from children to parents really works. And I also ran into some recursion problem, which made the code in the end quite inefficient.
The Question now is: Can I do this downscaling of a child element directly/easily in XAML?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信 Viewbox 会做你想做的事想。它有一个子项,该子项始终被赋予所需的大小,并且它将 ScaleTransform 应用于该子项,以便它适合 Viewbox 内。默认情况下,它将应用均匀拉伸,但您可以通过设置 拉伸属性。
像这样的事情:
I believe a Viewbox will do what you want. It has a single child which is always given its desired size, and it applies a ScaleTransform to the child so that it fits inside the Viewbox. By default it will apply a uniform stretch, but you can change this by setting the Stretch property.
Something like this:
是的,将重复元素的 VerticalStretch 设置为 Stretch,这应该会自动使它们表现为列表的一部分。
至于列表,如果它从容器中溢出,请将其放入 Dockpanel 中。
抱歉,我无法理解您在使用比例变换做什么:) ...但这听起来确实像是在敲螺丝。
如果这不能帮助我们理解,也许可以发布屏幕截图。
Yeah Set VerticalStretch for your repeating element to Stretch, that should automatically make them behave as part of the list.
As for the list, if it blows out of your container, put it in a Dockpanel.
Sorry I couldn't understand what you're doing with the scale transform :) ... but it does sound like hammering a screw in.
Maybe post a screenshot if that doesn't do it to help us understand.