如果我将Column的宽度设置为*,则它们最初的宽度相同,但如果某个项目大于允许的数量,则它将拉伸列宽.
如何通过明确定义大小来强制我的Grid保持其列的大小相同?
我不能使用UniformGrid,因为这个Grid正在ItemsControl中使用,而Items需要放在特定的Grid.Row/ Grid.Columnspot中
编辑这是我当前代码的示例.
<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>
Run Code Online (Sandbox Code Playgroud)
编辑#2这是我的最终解决方案.它使列的大小正确,并在应用程序调整大小时保持大小正确.
<ColumnDefinition Width="{Binding
ElementName=RootControl,
Path=ActualWidth,
Converter={StaticResource MathConverter},
ConverterParameter=(@VALUE-150)/7}" />
Run Code Online (Sandbox Code Playgroud)
150是行标题的宽度+所有边距和边框.我实际上正在更新我MathConverter的一个IMultiValueConverter所以我可以绑定两个参数(如果你对转换器代码感兴趣它可以在这里找到,虽然它只是单值转换器)
Lou*_*ann 10
你可以:
1)在DIP中硬编码大小:
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
...
Run Code Online (Sandbox Code Playgroud)
2)使用SharedSizeGroup,它需要一个char
<ColumnDefinition SharedSizeGroup="A" />
<ColumnDefinition SharedSizeGroup="A" />
...
Run Code Online (Sandbox Code Playgroud)
你可以在这里阅读更多相关信息
最干净的方法是使用UniformGrid这样的:
<UniformGrid Rows="1">
<Rectangle Fill="Blue" />
<Rectangle Fill="Yellow" />
<Rectangle Fill="Red" />
</UniformGrid>
Run Code Online (Sandbox Code Playgroud)
用作 时特别好ItemsPanel。