在多个 WPF 控件之间共享列宽 [英] Sharing Column Widths Between Multiple WPF Controls

查看:33
本文介绍了在多个 WPF 控件之间共享列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在控件之间共享列宽,而不仅仅是在同一控件上的多个网格之间共享?

Is there any way of sharing column widths between controls, not just between multiple grids on the same control?

我试图得到的粗略图:

我目前正在弄乱获取每个 UserControl 第一列中标签的宽度,但这似乎是一个很麻烦的解决方案,它占用了大量 CPU(在呈现之前查找标签并计算文本的宽度是讨厌!).

I'm currently messing around with getting the widths of the labels in the first column of each UserControl, but it seems a messy solution that is quite CPU heavy (finding the labels and calculating the widths of the text before it's rendered is nasty!).

我尽可能多地阅读了有关 SharedSizeGroups 的信息,但没有任何迹象表明它们可以跨不同控件工作.有没有一个简单的解决方案,或者更简单的解决方案,而不是完全可怕的?!

I've been reading up on as much about SharedSizeGroups as I can find, but there's nothing suggesting they work across different controls. Is there a simple solution or even a less simple one that isn't utterly hideous?!

推荐答案

之前的两个答案都是正确的.这是一个示例(主要取自 MSDN) 关于如何通过在父容器上设置 Grid.IsSharedSizeScope="True" 在两个不同的 UserControl 上使用它.请注意 ColumnDefinition 上的 SharedSizeGroup 属性.您可以通过切换 Grid.IsSharedSizeScope True/False 来查看效果

Both previous answers are correct. Here's an example (taken mostly from MSDN) on how you can use this on two different UserControls by setting Grid.IsSharedSizeScope="True" on the parent container. Notice the SharedSizeGroup attribute on the ColumnDefinition. You can see the effect by toggling Grid.IsSharedSizeScope True/False

主窗口

<StackPanel Grid.IsSharedSizeScope="True">
    <my:UserControl1 HorizontalAlignment="Left" x:Name="userControl11" />
    <my:UserControl2 HorizontalAlignment="Left" x:Name="userControl21" />
</StackPanel>

UserControl1

<UserControl ...>
    <Grid ShowGridLines="True" Margin="0,0,10,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition SharedSizeGroup="FirstColumn"/>
            <ColumnDefinition SharedSizeGroup="SecondColumn"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" SharedSizeGroup="FirstRow"/>
        </Grid.RowDefinitions>

        <Rectangle Fill="Silver" Grid.Column="0" Grid.Row="0" Width="200" Height="100"/>
        <Rectangle Fill="Blue" Grid.Column="1" Grid.Row="0" Width="150" Height="100"/>

        <TextBlock Grid.Column="0" Grid.Row="0" FontWeight="Bold">First Column</TextBlock>
        <TextBlock Grid.Column="1" Grid.Row="0" FontWeight="Bold">Second Column</TextBlock>
    </Grid>

</UserControl>

UserControl2

<UserControl ...>
    <Grid ShowGridLines="True" Margin="0,0,10,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition SharedSizeGroup="FirstColumn"/>
            <ColumnDefinition SharedSizeGroup="SecondColumn"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" SharedSizeGroup="FirstRow"/>
        </Grid.RowDefinitions>

        <Rectangle Fill="Silver" Grid.Column="0" Grid.Row="0" />
        <Rectangle Fill="Blue" Grid.Column="1" Grid.Row="0" />

        <TextBlock Grid.Column="0" Grid.Row="0" FontWeight="Bold">First Column</TextBlock>
        <TextBlock Grid.Column="1" Grid.Row="0" FontWeight="Bold">Second Column</TextBlock>
    </Grid>
</UserControl>

这篇关于在多个 WPF 控件之间共享列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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