ScatterViewItem 可以动态调整大小以适应其内容吗?

发布于 2024-08-26 19:30:50 字数 312 浏览 2 评论 0原文

我们在 ScatterViewItem 内部托管一个控件,该控件由于添加、删除和/或折叠各种子元素而在运行时动态更改其大小。我们希望托管 ScatterViewItem 也能调整大小以正确适应其子控件,但我们很难实现这一点。我们尝试了许多不同的方法,最接近的方法是挂钩子控件的 SizeChanged 事件并显式设置 ScatterViewItem 的宽度和高度,但这种方法仍然存在问题。

当您在 XAML 中定义 ScatterViewItem 及其内容时,ScatterViewItem 的大小会适当调整。当内容大小发生变化时,我们希望在运行时强制执行相同的行为。任何帮助将不胜感激。谢谢!

We're hosting a control inside of a ScatterViewItem that dynamically changes its size at runtime as a result of having various sub-elements added, removed, and/or collapsed. We would like to have the hosting ScatterViewItem resize as well to properly fit its child controls, but we're having a hard time making this happen. We've tried a number of different things and the closest we've come is hooking into the child control's SizeChanged event and explicitly setting the ScatterViewItem's Width and Height, but this approach still has issues.

When you define a ScatterViewItem and its content in XAML, the ScatterViewItem is sized appropriately. We would like to force the same behavior at runtime when its content changes size. Any help would be greatly appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情独悲 2024-09-02 19:30:50

您是否尝试过设置此处描述的样式:

http://msdn.microsoft.com/ en-us/library/ee957369.aspx

在“绑定到内容大小”下?

绑定到内容大小

如 ScatterView 概述中所述,默认情况下有一个 ScatterViewItem
不一定会扩大或缩小其内容的大小。
您可以显式设置高度和宽度属性
ScatterViewItem,但有时您的内容可能是一个控件
大小未知,或者您的内容可能具有可变大小。

在这种情况下,我们建议您绑定尺寸
ScatterViewItem 到内容的维度。为此,您需要
定义一个 Style 对象(通常在你的资源部分
主应用程序窗口)。下面的代码示例显示了一个样式
可以应用于 ScatterViewItem 控件的对象声明
使其绑定到其内容的尺寸。

<Style x:Key="ScatterViewItemStyle" TargetType="{x:Type s:ScatterViewItem}">
    <Setter Property="MinWidth" Value="{Binding Path=Content.MinWidth, RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
    <Setter Property="MinHeight" Value="{Binding Path=Content.MinHeight, RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
    <Setter Property="MaxWidth" Value="{Binding Path=Content.MaxWidth, RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
    <Setter Property="MaxHeight" Value="{Binding Path=Content.MaxHeight, RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
    <Setter Property="Width" Value="{Binding Path=Content.Width, RelativeSource={RelativeSource Self}, Mode=TwoWay}"/>
    <Setter Property="Height" Value="{Binding Path=Content.Height, RelativeSource={RelativeSource Self}, Mode=TwoWay}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type s:ScatterViewItem}">
                <ContentPresenter />
            </ControlTemplate>
        </Setter.Value>
    </Setter> </Style>

当您想要创建一个使用尺寸的 ScatterViewItem 时
其内容,将样式应用到 ScatterViewItem,如下所示
以下代码示例。

<s:ScatterViewItem Style="{StaticResource ScatterViewItemStyle}">
     <Rectangle  Height="250" Width="250" Fill="Red" /> </s:ScatterViewItem>

Have you tried setting the style described here:

http://msdn.microsoft.com/en-us/library/ee957369.aspx

under "Binding to Content Size"?

Binding to Content Size

As described in ScatterView Overview, by default a ScatterViewItem
does not necessarily expand or contract to the size of its content.
You can explicitly set the Height and Width properties of a
ScatterViewItem, but sometimes your content may be a control of an
unknown size, or your content might have variable size.

In cases like this, we recommend that you bind the dimensions of the
ScatterViewItem to the dimensions of the content. To do so, you need
to define a Style object (usually within the Resources section of your
main application window). The following code example shows a Style
object declaration that you can apply to a ScatterViewItem control to
cause it to bind to the dimensions of its content.

<Style x:Key="ScatterViewItemStyle" TargetType="{x:Type s:ScatterViewItem}">
    <Setter Property="MinWidth" Value="{Binding Path=Content.MinWidth, RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
    <Setter Property="MinHeight" Value="{Binding Path=Content.MinHeight, RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
    <Setter Property="MaxWidth" Value="{Binding Path=Content.MaxWidth, RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
    <Setter Property="MaxHeight" Value="{Binding Path=Content.MaxHeight, RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
    <Setter Property="Width" Value="{Binding Path=Content.Width, RelativeSource={RelativeSource Self}, Mode=TwoWay}"/>
    <Setter Property="Height" Value="{Binding Path=Content.Height, RelativeSource={RelativeSource Self}, Mode=TwoWay}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type s:ScatterViewItem}">
                <ContentPresenter />
            </ControlTemplate>
        </Setter.Value>
    </Setter> </Style>

When you want to create a ScatterViewItem that uses the dimensions of
its content, apply the style to the ScatterViewItem as shown in the
following code example.

<s:ScatterViewItem Style="{StaticResource ScatterViewItemStyle}">
     <Rectangle  Height="250" Width="250" Fill="Red" /> </s:ScatterViewItem>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文