WPF XAML:如何在 XAML 中自动缩小组件的子级? [英] WPF XAML: How to automatically scale down children of a component in XAML?

查看:38
本文介绍了WPF XAML:如何在 XAML 中自动缩小组件的子级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:

xaml 文件 1:模板化列表控件

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 文件 2:项目模板

xaml file 2: the item template

 <Border BorderBrush="Black" BorderThickness="1">
    <Border Child="{Binding Path=., Converter={StaticResource DynaUIConverter}}"/>
 </Border>

问题是我对 myUIrelatedDataList 以及 DynaUIConverter 生成的 UIElements 的类型一无所知.DynaUIConverter 通常会生成一组相互嵌套并包含文本框、按钮等的 Stackpanel.这些面板没有设置 Width 或 Height,我可能无法修复.

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.

我认为我需要测量生成的 UI 的最小尺寸要求,并动态地将 ScaleTransform 应用于生成的 UI,或者更好地应用于包含生成的 UI 的 Border.

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.

现在的问题是:我可以在 XAML 中直接/轻松地缩小子元素吗?

推荐答案

我相信 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.

像这样:

<ItemsControl ItemsSource="{Binding myUIrelatedDataList}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Viewbox Width="160" Height="80">
                <my:DynamicUIPresenter />
            </Viewbox>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这篇关于WPF XAML:如何在 XAML 中自动缩小组件的子级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆