我正在学习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设计器中,除了第一个单元格(即带有内联模板的单元格)外,一切都正确显示
有没有人有建议。
N.B。 “内联”模板主要用于测试目的。
编辑: 这样做(见下文)而不是使用ContentPresenter工作正常。也许我以错误的方式使用ContentPresenter?
<StackPanel Grid.Row="0" Grid.Column="0">
<TextBlock Text="{Binding DaysOfWeek[0]}"/>
</StackPanel>
我想使用ContentPresenter的原因是因为每个单元格内容的DataTemplate最终不仅仅是一个文本框。
答案 0 :(得分:16)
尝试将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]}"/>
答案 1 :(得分: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" />