Setting Datacontext in contentpresenter: binding inside ContentTemplate does not work

I am learning WPF and the MVVM pattern, and I'm trying to create a calendar view. So currently I have a grid with 6 rows and 7 columns. The first line should be a heading, indicating in this way the days of the week, like "Monday, Tuesday, etc." I have the following right now in my 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"/> 

On like this: You see the pattern that I assume.

Here is CalendarHeaderCellTemplate

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

And here are the important parts of the 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()}; } 

Now, neither the Contentpresenter, where I define the DataTemplate 'inline', displays anything inside its TextBlock or CalendarHeaderCellTemplate.

It's funny that inside the Visual Studio designer everything is displayed correctly, except for the first cell (i.e. with an integrated template)

Does anyone have a suggestion.

NB The inline template was mainly made for testing purposes.

EDIT: Doing this (see below) instead of using ContentPresenter works fine. Maybe I'm using ContentPresenter incorrectly?

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

The reason I want to use ContentPresenter is because in the end, the DataTemplate for the contents of each cell will be much more than just a text field.

+4
source share
2 answers

Try changing ContentPresenter to

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

or

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

and also replace your DataContext with content like

 <ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="1" Content="{Binding DaysOfWeek[1]}"/> 
+13
source

try using ContentControl rather than 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" /> 
+4
source

Source: https://habr.com/ru/post/1333273/


All Articles