在 WPF 中将网格行的高度与其内容绑定

发布于 2024-07-20 06:00:12 字数 1345 浏览 4 评论 0原文

我有一个有几行的网格。 在顶行中,我有一个动态绑定到集合的 ItemsControl,并使用 DataTemplateSelector 和 ItemsPanelTemplate(带有单个水平排列的 WrapPanel)。 这是我到目前为止所拥有的内容的精简版本:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40" />
        <RowDefinition Height="2" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <GridSplitter Background="#666" Grid.Row="1" Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
    <ItemsControl x:Name="items" Background="#DDD" Grid.Row="0" ItemTemplateSelector="{StaticResource itemTemplateSelector}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</Grid>

我不想将第一行的高度设置为 40,而是根据 ItemsControl 必须适合其所有内容的最小高度动态设置它。 由于 ItemsControl 的高度受到网格行高度的限制,因此我无法确定应该绑定哪个元素以及应该将其绑定到哪个元素的属性。

将网格行的高度绑定到 ItemsControl 的“所需高度”是有意义的。 因此,我找到了 DesiredSize 属性并将 RowDefinition 的高度绑定到 ItemsControl 的 DesiredSize.Height。 它在第一次加载时有效,但在我调整控件大小时不会更新(请记住,我使用 WrapPanel 作为我的 ItemsPanelTemplate,因此当我调整窗口大小时,ItemsControl 的高度应该发生变化)。

有谁知道绑定框架是否支持这种情况,或者我是否需要添加事件处理程序代码来完成此任务?

谢谢。

I have a grid with a few rows. In the top row, I have an ItemsControl that is bound dynamically to a collection and uses a DataTemplateSelector and ItemsPanelTemplate (with a single horizontally arranged WrapPanel). Here's a stripped-down version of what I have so far:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40" />
        <RowDefinition Height="2" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <GridSplitter Background="#666" Grid.Row="1" Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
    <ItemsControl x:Name="items" Background="#DDD" Grid.Row="0" ItemTemplateSelector="{StaticResource itemTemplateSelector}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</Grid>

Instead of setting that first row's height to 40, I'd like to set it dynamically based on the minimum height that the ItemsControl must be to fit all of its contents. Since the ItemsControl's height is being constrained by the Grid Row's height, I can't figure out which element I should be binding and to what element's property I should be binding it to.

It would make sense to bind the Grid Row's Height to the "desired height" of the ItemsControl. So, I found the DesiredSize property and bound my RowDefinition's Height to the ItemsControl's DesiredSize.Height. That works when it first loads, but does not update as I resize the control (remember, I'm using a WrapPanel as my ItemsPanelTemplate, so as I resize the window, the height of the ItemsControl should be changing).

Does anyone know if this kind of situation is even supported by the binding framework, or would I need to add event handler code to accomplish this?

Thanks.

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

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

发布评论

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

评论(1

等数载,海棠开 2024-07-27 06:00:12

您是否尝试将高度设置为“自动”来实现您想要的:(

<RowDefinition Height="Auto"/>

或者我在这里想得太简单了..?)

Did you try setting height to Auto to achieve what you want:

<RowDefinition Height="Auto"/>

(or am i thinking too simple here.. ?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文