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)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…