将ListBox与Data在同一行上分组

时间:2016-12-13 11:56:19

标签: c# wpf listbox listcollectionview

我有一个ListBox,它绑定到名为“Schedules”的ListCollectionView。

<ListBox Grid.Row="2" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding Schedules}">
    <ListBox.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ListBox.GroupStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Background="Transparent">
                <Grid Background="Transparent">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition  Width="100"/>
                        <ColumnDefinition  Width="100"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <!--I want my DateTime Grouping Here basically-->
                    <!--<TextBlock Grid.Column="0" Grid.Row="0"  Text="{Binding MyDateTime}" HorizontalAlignment="Left"  VerticalAlignment="Center"/>-->
                    <TextBlock Grid.Column="1" Grid.Row="0"  Text="{Binding MyDateTime, StringFormat='t'}" HorizontalAlignment="Left"  VerticalAlignment="Center"/>
                    <TextBlock Grid.Column="2" Grid.Row="0"  Text="{Binding SomeText}"  TextTrimming="WordEllipsis" LineStackingStrategy="MaxHeight" MaxHeight="20" HorizontalAlignment="Left"  VerticalAlignment="Center"/>
                </Grid>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

在我的viewmodel中,我只按日期对它进行分组(我使用的ValueConverter只给了我一个日期:

Schedules.GroupDescriptions.Add(new PropertyGroupDescription("MyDateTime", new DateTimeToDateOnlyConverter()));

是否可以进行分组但是将其显示在与其他数据相同的行上? 现在你得到一个案例:

MyDateTime
-----------------时间| Sometext
-----------------时间| Sometext
-----------------时间| Sometext
MyDateTime2
-----------------时间| Sometext

我希望它显示如下:

MyDateTime |时间| Sometext
|时间| Sometext
|时间| Sometext
MyDateTime |时间| Sometext
|时间| Sometext
|时间| Sometext

1 个答案:

答案 0 :(得分:0)

将grid.rowspan添加到xaml以合并行

<TextBlock Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Text="{Binding MyDateTime}" HorizontalAlignment="Left" VerticalAlignment="Center"/>