For a very custom calendar control, I need to have a week object and bind the Day objects within that week to a grid. I figured I would have the DayOfWeek enum decide where the day should go within the grid. That way if the month starts on a Tuesday it'll have the property Grid.Column="2". But for some reason all of them end up in the first column and I don't know why.
<ItemsControl ItemsSource="{Binding Weeks}" SnapsToDevicePixels="True">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Days}"> <!--7 most of the time-->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="28" />
<ColumnDefinition Width="28" />
<ColumnDefinition Width="28" />
<ColumnDefinition Width="28" />
<ColumnDefinition Width="28" />
<ColumnDefinition Width="28" />
<ColumnDefinition Width="28" />
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>开发者_开发百科;
<TextBlock Text="{Binding Date.Day}" Grid.Column="{Binding DayOfWeekInt}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
And I have that binding to a property on the day object like so:
public int DayOfWeekInt
{
get { return (int)Date.DayOfWeek; }
}
Any Ideas?
It turns out the ItemsControl was wrapping the TextBlock with a ContentPresenter, masking the Grid.Column on the TextBlock. This can alleviated by setting a style on the ContentPresenter:
<ItemsControl.Resources>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="Grid.Column" Value="{Binding Path=DayOfWeekInt}" />
</Style>
</ItemsControl.Resources>
精彩评论