views:

604

answers:

2

I have a list view that is displaying data using the gridview. This list is displaying data typical of grid views. Lots of data displayed in rows and columns. However, on some rows I do not have any data to display, in the columns. I would like to swap out the item template for that row and display another template for the entire row that would display something like "blank row" or "empty row" for the text and also style the whole row not the individual cells.

I have been trying to use this post: http://cloudstore.blogspot.com/2008/06/creating-custom-view-mode-that-supports.html.

So I have a custom view for the list view. Basically, I have subclassed the GridView class and on the PrepareItems method override, I can select the new template that I want to display for a particular row or the default template. However, when I try to select a new template, the gridview will never render anything for that row. It will still render if I use the default template. I assume that the layout for a GridView is interfering with my setting of the template for the row I want to swap out. Is this possible or will I have to create a custom view that mimics the GridView and not subclass the GridView class? I would like to keep the columns the way they are without too much rework. Any suggestions or anyone have experience with this type of scenario?

+1  A: 

You can use DataTriggers. Use code like this (switching templates depending on if value of SomeItemProperty is 0 or not):

<DataTemplate x:Key="MyItemTemplate">
    <StackPanel>
        <StackPanel Name="normalStackPanel">
            <!-- template for normal row -->
        </StackPanel>
        <StackPanel Name="emptyStackPanel" Visibility="Collapsed">
            <!-- template for empty row -->
        </StackPanel>
    </StackPanel>

    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding SomeItemProperty}" Value="0">
            <Setter TargetName="normalStackPanel" Property="Visibility" Value="Collapsed" />
            <Setter TargetName="emptyStackPanel" Property="Visibility" Value="Visible" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

Hope it helps.

levanovd
+1  A: 

Okay here is what I did to solve my problem, however it will incur some rework on my part. I replaced with my listview with an items control. So I am not using the GridView of the list view anymore. Instead I defined a GridViewColumnCollection with my columns set up with how I wanted them.

For my header I used a GridViewHeaderRowPresenter with its columns property bound to GridViewColumnCollection. This takes care of the Column Headers.

Underneath my header I insert the ItemsControl which is bound to my items list. However, I now use a DataTemplateSelector that will swap the template out depending on the item and its properties. If you are unfamiliar with the DataTemplate Selector go here: data template selector

Basically, my default template for items in the list is a GridViewRowPresenter which its columns property is bound to GridViewColumnCollection. This keeps it in line with the headers. However, I can still swap out the template for a row to be something else through the Data Template Selector. And this solves my problem.

However, I now have to rework out of a listview with a gridview that I had styled, and use this method. So this does not come without some cost.

prem_data