开发者

Binding DayOfWeek enum to Custom Calendar Control Grid

开发者 https://www.devze.com 2023-01-14 22:37 出处:网络
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

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消