如何使我的网格列始终具有相同的宽度?
如果我将列的宽度设置为 *
,它们最初的宽度相同,但如果某个项目大于允许的数量,那么它将拉伸列宽度。
如何通过显式定义大小来强制我的网格保持其列的大小相同?
我无法使用 UniformGrid,因为此网格正在 ItemsControl 中使用,并且项目需要放置在特定的 Grid.Row
/Grid.Column
点
编辑 这是我当前代码的示例。
<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 这是我的最终解决方案。它使列具有正确的大小,并且在调整应用程序大小时保持正确的大小。
<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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以:
1) 在 DIP 中硬编码大小:
2) 使用 SharedSizeGroup,它需要一个 char
您可以阅读有关它的更多信息 此处
You could:
1) Hardcode a size in DIP:
2) Use SharedSizeGroup, it takes a char
You can read more about it here
您可以尝试将列的宽度绑定到一个属性,该属性将窗口的总宽度除以列数
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
最干净的方法是使用像这样的
UniformGrid
:用作
ItemsPanel
时效果非常好。The cleanest way is to use a
UniformGrid
like this:Extra nice when used as
ItemsPanel
.尝试网格的 IsSharedSizeScope 工作:
我希望它有所帮助。
有关详细说明,请查看此链接:
https://wpf.2000things.com/tag/sharedsizegroup/
Try the IsSharedSizeScope working of the Grid:
I hope it helps.
For the detail description check this link:
https://wpf.2000things.com/tag/sharedsizegroup/
我没有尝试过,但我认为这应该有效:
XAML:
C#:
I have not tried it, but I think this should work:
XAML:
C#: