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> <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]}"/> <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.