在 DataGrid WPF 中的某些条件后添加分隔符

Add a separator after some condition in DataGrid WPF

我正在聊天 window,当新日期出现在绑定为数据网格项目源的列表的特定列中时,我想在其中添加水平分隔符。有可能吗?如果是的话怎么办? 任何帮助将不胜感激。

这是我想要做的:

刚刚检查了一下,是的,你可以分组:

a) 向表示消息的 object 添加一个新的 属性。 属性 将 return 一个字符串值,具体取决于日期(类似于 DayOfWeek.ToString();

b) 将您的 collection 分组为这个新的 属性(使用 <CollectionViewSource>

c) 为组设置样式,大致如下(您需要添加 canvas 或带有线条的内容,并可能将 DockPanel 的宽度绑定到它的 parent 宽度):

  <DataGrid.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Margin" Value="0,0,0,5"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <StackPanel>
                                        <DockPanel LastChildFill="True">
                                            <TextBlock Text="{Binding Path=Name}" DockPanel.Dock="Right"/>
                                            <!-- here comes the line -->
                                        </DockPanel>
                                        <ListBox>
                                            <ListBox.Items>
                                                <ItemsPresenter />
                                            </ListBox.Items>
                                        </ListBox>
                                    </StackPanel>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>              
        </DataGrid.GroupStyle>

P.S。如果您想将超过一周的消息以相同的模式分组,那么它当然不会起作用。在这种情况下,您可以尝试按两个属性进行分组。不过我会把所有东西都放在 "older" 中。