如何使我的网格列始终具有相同的宽度? [英] How can I make my Grid Columns always be the same width?

查看:41
本文介绍了如何使我的网格列始终具有相同的宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将列的宽度设置为 *,它们最初的宽度相同,但如果一个项目大于允许的数量,那么它会拉伸列的宽度.

If I set the Column's width to *, they're the same width initially but if an item is larger than the amount allowed then it will stretch the column width.

如何通过明确定义大小来强制我的 Grid 保持其列的大小相同?

How can I force my Grid to keep it's columns the same size with explicitly defining a size?

我不能使用 UniformGrid,因为这个 Grid 被用在一个 ItemsControl 中,并且 Items 需要放置在特定的 Grid.Row/Grid.Column

I cannot use a UniformGrid because this Grid is being used in an ItemsControl, and the Items need to be placed in specific Grid.Row/Grid.Column spots

编辑 这是我当前代码的示例.

Edit Here's a sample of my current code.

<DockPanel>

    <!-- Not showing code here for simplicity -->
    <local:ColumnHeaderControl DockPanel.Dock="Top" />
    <local:RowHeaderControl DockPanel.Dock="Left" />

    <ItemsControl ItemsSource="{Binding Events}">
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Grid.Column" 
                        Value="{Binding DueDate.DayOfWeek, 
                            Converter={StaticResource EnumToIntConverter}}" />
            </Style>
        </ItemsControl.ItemContainerStyle>

        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                </Grid>
            </ItemsPanelTemplate>
        </ItemsPanel>
    </ItemsControl>

</DockPanel>

编辑 #2 这是我的最终解决方案.它使列的大小正确,并在应用程序调整大小时保持大小正确.

Edit #2 Here's my final solution. It makes the columns the correct size, and it keeps the size correct when the application gets resized.

<ColumnDefinition Width="{Binding 
    ElementName=RootControl, 
    Path=ActualWidth, 
    Converter={StaticResource MathConverter}, 
    ConverterParameter=(@VALUE-150)/7}" />

150 是行标题 + 所有边距和边框的宽度.我实际上正在将我的 MathConverter 更新为 IMultiValueConverter 以便我可以绑定这两个参数(如果您对转换器代码感兴趣,可以在 这里,虽然只是单值转换器)

150 is the width of the Row Headers + all margins and borders. I'm actually in the process of updating my MathConverter to an IMultiValueConverter so I can bind both parameters (If you're interested in the Converter code it can be found here, although it's only the single-value converter)

推荐答案

您可以尝试将列的宽度绑定到一个属性,该属性将窗口的总宽度除以列数

You could try binding the width of your columns to a property that divides the total width of the window by the number of columns

这篇关于如何使我的网格列始终具有相同的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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