Wpf listview/gridview: hiding repeated content

listviewwpf

I'm new to WPF and don't know how to implement what I need.

My problem:

I've got a data bound WPF gridview displaying the results of an SQL query.
Some of the columns present repeated data that I'd like to hide.
This:

occupancy   name       room
---------   ---------  -----
Occupied    Bob        1
Occupied    Ted        2
Available              3
Available              4

Should look like this:

occupancy   name       room
---------   ---------  -----
Occupied    Bob        1
            Ted        2
Available              3
                       4

My environment:

Using C# on .net 3.5. I've bound the listview to a dataview collection. I have a data template for the cells to control the formatting.

My tenative solution:

I thought I should apply a trigger to the template and change the properties to make the text empty on all rows after the first where the data is the same as the previous row.

When I setup the trigger can I perform this all in xaml or do I need to write code to access the previous row?

If it's in XAML how do you construct the binding string(s) to reference the columns in the current and previous rows of the dataview it's bound to? Will it choke on the first row since there is no previous row?

here's the template I'm working with:

<DataTemplate x:Key="Occupancy">
    <TextBlock Name="Occupancy" Text="{Binding Path=Occupancy}" TextAlignment="Left" />
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=Occupancy}">
            <DataTrigger.Value>Available</DataTrigger.Value>
            <Setter TargetName="Occupancy" Property="**{previous column's content}**" Value="**{current columns content}**"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

Maybe use RelativeSource:PreviousData ??

If I have to write code does anyone know of a decent tutorial that would get me started?

Alternative solutions are ok, though I think a no-code style that I could apply to many columns would be the optimal solution.

Thanks for your time.

Jay

Best Answer

I've had a negative experience with the RelativeSource:PreviousData. It worked in ListBox, but in the list view I was getting an error “Cannot find previous element for use as RelativeSource because there is no parent in generated context.”

I finally gave up and enumerated rows myself (the idea was to use it to print row numbers).

In your situation, I'd use a ModelView with a list elements of which would point to corresponding and previous elements of your Model. So if previous and current have the same value in the corresponding property, ModelView's element would return null.

Related Topic