8

我正在学习 WPF 和 MVVM 模式,并且正在尝试构建类似日历的视图。所以我目前有一个 6 行 7 列的网格。第一行应该是标题,因此指定工作日,例如“星期一、星期二等......”我现在在 MonthView.xaml 中有以下内容

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="{Binding RowHeight}"/>
        <RowDefinition Height="{Binding RowHeight}"/>
        <RowDefinition Height="{Binding RowHeight}"/>
        <RowDefinition Height="{Binding RowHeight}"/>
        <RowDefinition Height="{Binding RowHeight}"/>
        <RowDefinition Height="{Binding RowHeight}"/>
        <RowDefinition Height="{Binding RowHeight}"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{Binding CellWidth}"/>
        <ColumnDefinition Width="{Binding CellWidth}"/>
        <ColumnDefinition Width="{Binding CellWidth}"/>
        <ColumnDefinition Width="{Binding CellWidth}"/>
        <ColumnDefinition Width="{Binding CellWidth}"/>
        <ColumnDefinition Width="{Binding CellWidth}"/>
        <ColumnDefinition Width="{Binding CellWidth}"/>
    </Grid.ColumnDefinitions>

    <!-- Header Row-->
    <ContentPresenter Grid.Row="0" Grid.Column="0">
        <ContentPresenter.ContentTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding DaysOfWeek[0]}"/>
            </DataTemplate>
        </ContentPresenter.ContentTemplate>
    </ContentPresenter>
    <ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="1" DataContext="{Binding DaysOfWeek[1]}"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="2" DataContext="{Binding DaysOfWeek[2]}"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="3" DataContext="{Binding DaysOfWeek[3]}"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="4" DataContext="{Binding DaysOfWeek[4]}"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="5" DataContext="{Binding DaysOfWeek[5]}"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="6" DataContext="{Binding DaysOfWeek[6]}"/>

    <!-- 1st Row-->
    <ContentPresenter ContentTemplate="{StaticResource CalendarCellTemplate}" Grid.Row="1" Grid.Column="0"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarCellTemplate}" Grid.Row="1" Grid.Column="1"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarCellTemplate}" Grid.Row="1" Grid.Column="2"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarCellTemplate}" Grid.Row="1" Grid.Column="3"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarCellTemplate}" Grid.Row="1" Grid.Column="4"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarCellTemplate}" Grid.Row="1" Grid.Column="5"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarCellTemplate}" Grid.Row="1" Grid.Column="6"/>

依此类推:你看我猜的模式。

这是 CalendarHeaderCellTemplate

<DataTemplate x:Key="CalendarHeaderCellTemplate">
    <StackPanel Margin="5" Background="Blue">
        <TextBlock Text="{Binding}"></TextBlock>
    </StackPanel>
</DataTemplate>

这是 ViewModel 的重要部分:

 public ObservableCollection<string> DaysOfWeek { get; private set; }
 public MonthViewModel()
    {  
        this.DaysOfWeek = new ObservableCollection<string> {DayOfWeek.Monday.ToString(), DayOfWeek.Tuesday.ToString(), DayOfWeek.Wednesday.ToString(), 
            DayOfWeek.Thursday.ToString(), DayOfWeek.Friday.ToString(), DayOfWeek.Saturday.ToString(), DayOfWeek.Sunday.ToString()};

    }

现在,我定义 DataTemplate 'inline' 的 Contentpresenter 都不会在其 TextBlock 和 CalendarHeaderCellTemplate 内显示任何内容。

有趣的是,在 Visual Studio 设计器中,一切都正确显示,除了第一个单元格(即带有内联模板的单元格)

有没有人有建议。

注意“内联”模板主要用于测试目的。

编辑:这样做(见下文)而不是使用 ContentPresenter 工作正常。也许我以错误的方式使用 ContentPresenter?

 <StackPanel Grid.Row="0" Grid.Column="0">
     <TextBlock Text="{Binding DaysOfWeek[0]}"/>
 </StackPanel>

我想使用 ContentPresenter 的原因是因为每个单元格内容的 DataTemplate 最终将不仅仅是一个文本框。

4

2 回答 2

24

尝试将您的 ContentPresenter 更改为

    <ContentPresenter Content="{Binding DaysOfWeek[0]}" Grid.Row="0" Grid.Column="0">
        <ContentPresenter.ContentTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
        </ContentPresenter.ContentTemplate>
    </ContentPresenter>

或者

  <ContentPresenter Content="{Binding}" Grid.Row="0" Grid.Column="0">
        <ContentPresenter.ContentTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding DaysOfWeek[0]}"/>
            </DataTemplate>
        </ContentPresenter.ContentTemplate>
    </ContentPresenter>

并将您的 DataContext 替换为 Content 之类的

<ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="1" Content="{Binding DaysOfWeek[1]}"/>
于 2010-12-24T08:39:36.633 回答
5

尝试使用 ContentControl 而不是 ContentPresenter

<Style x:Key="CalendarCell" TargetType="ContentControl">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ContentControl">
        <StackPanel Margin="5" Background="{TemplateBinding Background}">
          <TextBlock Text="{TemplateBinding Content}" 
                     Foreground="{TemplateBinding Foreground}" />
        </StackPanel>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

<!-- Header Row-->
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="{Binding DaysOfWeek[0]}" 
                Background="Blue" 
                Foreground="White" />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="{Binding DaysOfWeek[1]}" 
                Background="Blue" 
                Foreground="White" 
                Grid.Column="1" />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="{Binding DaysOfWeek[2]}" 
                Background="Blue" 
                Foreground="White" 
                Grid.Column="2"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="{Binding DaysOfWeek[3]}" 
                Background="Blue" 
                Foreground="White" 
                Grid.Column="3"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="{Binding DaysOfWeek[4]}" 
                Background="Blue" 
                Foreground="White" 
                Grid.Column="4"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="{Binding DaysOfWeek[5]}" 
                Background="Blue" 
                Foreground="White" 
                Grid.Column="5"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="{Binding DaysOfWeek[6]}" 
                Background="Blue" 
                Foreground="White" 
                Grid.Column="6"  />

<!-- 1st Row-->
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="" 
                Background="Wheat" 
                Foreground="Black" 
                Grid.Row="1"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="" 
                Background="Wheat" 
                Foreground="Black" 
                Grid.Column="1" 
                Grid.Row="1"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="" 
                Background="Wheat" 
                Foreground="Black" 
                Grid.Column="2" 
                Grid.Row="1"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="" 
                Background="Wheat" 
                Foreground="Black" 
                Grid.Column="3" 
                Grid.Row="1"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="" 
                Background="Wheat" 
                Foreground="Black" 
                Grid.Column="4" 
                Grid.Row="1"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="" 
                Background="Wheat" 
                Foreground="Black" 
                Grid.Column="5" 
                Grid.Row="1"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="" 
                Background="Wheat" 
                Foreground="Black" 
                Grid.Column="6" 
                Grid.Row="1"  />
于 2010-12-24T08:57:30.137 回答