【问题标题】:Binding DayOfWeek enum to Custom Calendar Control Grid将 DayOfWeek 枚举绑定到自定义日历控件网格
【发布时间】:2010-09-03 13:54:42
【问题描述】:

对于一个非常自定义的日历控件,我需要一个周对象并将该周内的 Day 对象绑定到一个网格。我想我会让 DayOfWeek 枚举决定一天在网格中的位置。这样,如果该月从星期二开始,它将具有属性 Grid.Column="2"。但由于某种原因,它们都出现在第一列,我不知道为什么。

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

我将它绑定到 day 对象上的一个属性,如下所示:

public int DayOfWeekInt
{
   get { return (int)Date.DayOfWeek; }
}

有什么想法吗?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    原来 ItemsControl 用 ContentPresenter 包装了 TextBlock,掩盖了 TextBlock 上的 Grid.Column。这可以通过在 ContentPresenter 上设置样式来缓解:

    <ItemsControl.Resources>
        <Style TargetType="{x:Type ContentPresenter}">
            <Setter Property="Grid.Column" Value="{Binding Path=DayOfWeekInt}" />
         </Style>
    </ItemsControl.Resources>
    

    【讨论】:

      猜你喜欢
      • 2015-03-29
      • 2013-10-02
      • 2010-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 2015-10-17
      • 1970-01-01
      相关资源
      最近更新 更多