到目前为止一切顺利。现在我有每个课程的数据网格,我想要打印。那已经有效了。但是,我只想显示不同的单元格内容而不是许多单元格内容。
例如。在第1周到第5周的日历中,学生通过称为“course1”的课程。之后,他摒弃了不同课程名称的下一门课程。 DataGrid应显示所有52个日历周。对于那些具有相同内容(课程名称)的单元格(仅水平),它们应该合并/连接到一个大单元格(与ColumnSpan一样),只有一次其课程名称。
单元格应该如下所示(来自MS Excel):
这就是我的View的样子:
<controls:MetroTabItem Header="Zeitstrahl" controls:ControlsHelper.HeaderFontSize="15" MinWidth="600" >
<StackPanel>
<StackPanel>
<DataGrid x:Name="Zeitstrahl"
Margin="5 5 12 5"
SelectionUnit="FullRow"
RowHeaderWidth="45"
AutoGenerateColumns="False"
FrozenColumnCount="5"
VirtualizingPanel.IsVirtualizingWhenGrouping="True"
VirtualizingPanel.VirtualizationMode="Standard"
ItemsSource="{Binding PlanningTestModel.TestDataPlanning, UpdateSourceTrigger=PropertyChanged, IsAsync=True}"
behaviours:DataGridColumnsBehavior.BindableColumns="{Binding CoursePlanningColumns, UpdateSourceTrigger=PropertyChanged}">
<DataGrid.Style>
<Style TargetType="DataGrid" BasedOn="{StaticResource {x:Type DataGrid}}">
<Setter Property="AlternatingRowBackground" Value="Gainsboro"/>
</Style>
</DataGrid.Style>
</DataGrid>
<Button Content="Drucken" Width="70" DockPanel.Dock="Bottom" HorizontalAlignment="Right"
Command="{Binding PrintDataGridCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=Zeitstrahl, Mode=OneWay}" />
</StackPanel>
</StackPanel>
</controls:MetroTabItem>
在我的ViewModel中,我有一个IObservableCollection:
public TestModel PlanningTestModel { get; set; } = new TestModel();
public ObservableCollection<DataGridColumn> CoursePlanningColumns { get; private set; }
//Part of my GeneratePlanningColumns -method
foreach (string kw in PlanningTestModel.Weeks)
{
if (i < 52)
{
var selectedItemBinding = new Binding
{
Converter = courseUnitSelectedItemConverter,
Path = new PropertyPath(string.Format("Aw{0}", (i + 1))),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
var selectedColorBinding = new Binding
{
Converter = hexColorToBackgroundConverter,
Path = new PropertyPath(string.Format("Aw{0}", (i + 1))),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
Thickness thick = new Thickness(0);
var cellStyle = new Style(typeof(DataGridCell))
{
Setters =
{
new Setter(DataGridCell.BorderThicknessProperty,thick ),
new Setter(Control.BackgroundProperty, selectedColorBinding),
new Setter(FrameworkElement.HeightProperty, 24.0),
new Setter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Stretch),
new Setter(Control.VerticalContentAlignmentProperty, VerticalAlignment.Stretch),
new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center),
new Setter(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Stretch)
}
};
CoursePlanningColumns.Add(new DataGridTextColumn
{
Header = PlanningTestModel.Weeks[i],
HeaderStyle = headerStyle,
Binding = selectedItemBinding,
CellStyle = cellStyle,
IsReadOnly = true
});
TrainerPlanningColumns.Add(new DataGridTextColumn
{
Header = PlanningTestModel.Weeks[i],
HeaderStyle = headerStyle,
Binding = selectedItemBinding,
CellStyle = cellStyle,
IsReadOnly = true
});
}
i++;
}
这就是我用作行为的内容:How do I bind a WPF DataGrid to a variable number of columns?
感谢您的帮助!