actually I'm working on a project which will be able to plan some vocational trainings. For that I need an Output (to print on DIN A3/A2) of this plan.
So far so good. Now I have a datagrid for each course, which I want to print. That already works. But instead of many cell contents I only want to display different cell contents.
Eg. In calendar week 1 to 5 the student absolves a cours called "course1". After that he absolves the next courses with different course names. The DataGrid should show all 52 calendar weeks. For those Cells (only horizontally) which have the same content(course names) they should be merged/connected to one large cell (as with ColumnSpan) with only one time its course name.
The cells should look like this example (from MS Excel):
This is how my View looks like:
<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>
In my ViewModel I have an 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++;
}
This is what I've used as behavior: How do I bind a WPF DataGrid to a variable number of columns?
Thanks for your help!