我怎样才能使我的网格列总是相同的宽度? [英] How can I make my Grid Columns always be the same width?
问题描述
如果我将Column的宽度设置为 *
,它们最初的宽度相同,但如果一个项目大于允许的数量,那么它将拉伸列宽。
我如何强制Grid保持列的大小相同不能使用UniformGrid,因为这个Grid在ItemsControl中被使用,并且Items需要被放置在特定的 Grid.Row
/ Grid.Column
点
编辑下面是我当前的代码示例。
< DockPanel>
<! - 为简单起见,不在此显示代码 - >
< 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.ColumnDefinitions>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>
< / Grid>
< / ItemsPanelTemplate>
< / ItemsPanel>
< / ItemsControl>
< / DockPanel>
编辑#2 这是我的最终解决方案。它使列的大小正确,并且在应用程序调整大小时保持大小正确。
< ColumnDefinition Width = {Binding
ElementName = RootControl,
Path = ActualWidth,
Converter = {StaticResource MathConverter},
ConverterParameter =(@ VALUE-150)/ 7}/>
<150>是行标题+所有边距和边界的宽度。我实际上正在将我的 MathConverter
更新为 IMultiValueConverter
,这样我就可以绑定这两个参数(如果'对转换器代码感兴趣,可以在此处找到,虽然它只是单值转换器)解决方案您可以尝试将列的宽度绑定到一个属性,该属性将总数窗口的宽度乘以列数
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.
How can I force my Grid to keep it's columns the same size with explicitly defining a size?
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>
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 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屋!