Настройка Datacontext для contentpresenter: привязка внутри ContentTemplate не работает - PullRequest
5 голосов
/ 24 декабря 2010

Я изучаю WPF и шаблон MVVM и пытаюсь создать представление, похожее на календарь. Таким образом, в настоящее время у меня есть Grid с 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"/>

И так далее: вы видите шаблон, который я предполагаю.

Вот календарьHeaderCellTemplate

<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()};

    }

Теперь ни Contentpresenter, где я определяю «встроенный» объект DataTemplate, ничего не отображает внутри его TextBlock, ни CalendarHeaderCellTemplate.

Забавно, что внутри дизайнера Visual Studio все отображается правильно, за исключением первой ячейки (то есть ячейки со встроенным шаблоном)

Есть ли у кого-нибудь предложение.

N.B. «Встроенный» шаблон был в основном сделан для тестирования.

EDIT: Делать это (см. Ниже) вместо использования ContentPresenter работает нормально. Может быть, я неправильно использую ContentPresenter?

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

Причина, по которой я хочу использовать ContentPresenter, заключается в том, что DataTemplate для содержимого каждой ячейки в конечном итоге будет намного больше, чем просто текстовое поле.

Ответы [ 2 ]

16 голосов
/ 24 декабря 2010

Попробуйте изменить свой 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 на содержимое типа

<ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="1" Content="{Binding DaysOfWeek[1]}"/>
5 голосов
/ 24 декабря 2010

попробуйте использовать 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"  />
...