tags:

views:

1080

answers:

4

Hello world, 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

+2  A: 

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.

Sergey Aldoukhov
I tried to get the guys at Sqlite to add a builtin function to return the row number of the query result set but they didn't see the value in it ;) Thanks
Jay
+1  A: 

I don't have much experience with the WPF ListView (I tend to stick to the more basic controls/containers), but have you considered trying to first group the original data with a LINQ query?

Daniel Pratt
It's already grouped by the sql query. A coworker pointed out it would be a lot simpler to just remove the repeated content and put it into a heading. I may end up doing that. Go with the flow to get it done in a reasonable time. Thanks for the suggestion though.
Jay
+1  A: 

The previous data option only works with a ListView, and only if you don't have a GridView associated with it. I believe this may be because with a view hooked to the ListView control it doesn't create an ItemsControl collection. The code that finds the previous data checks for a previous item within the ItemsControl collection. This is just a guess though.

Jay
A: 

I have the same work to do. Can any one provide the code?

It does not work without lots of effort.
Jay