WPF:如何使用MVVM将DataGridCells与相同内容合并/连接?

时间:2018-03-20 10:15:35

标签: c# wpf merge datagrid datagridcell

实际上,我正在开展一个能够计划一些职业培训的项目。为此,我需要此计划的输出(在DIN A3 / A2上打印)。

到目前为止一切顺利。现在我有每个课程的数据网格,我想要打印。那已经有效了。但是,我只想显示不同的单元格内容而不是许多单元格内容。 例如。在第1周到第5周的日历中,学生通过称为“course1”的课程。之后,他摒弃了不同课程名称的下一门课程。 DataGrid应显示所有52个日历周。对于那些具有相同内容(课程名称)的单元格(仅水平),它们应该合并/连接到一个大单元格(与ColumnSpan一样),只有一次其课程名称。 The Datagrid

单元格应该如下所示(来自MS Excel):

Destination-Design

这就是我的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?

感谢您的帮助!

0 个答案:

没有答案