tags:

views:

229

answers:

2

I want to have ListView control which can be filled with one of a number of different object types (not mixed, always just one type). I would like to adjust the columns dynamically to correspond to the object type currently in the list.

I know I could do this programmatically (http://stackoverflow.com/questions/868204/adding-columns-programatically-to-listview-in-wpf) but I was wondering if I could define the different column descriptions (GridViews) in XAML as resources and then select the one I need at run-time when the object type changes?

Clarification I am using a ListView control which displays details in columns. The columns are defined like this:

<ListView x:Name="TheList">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Columns" Width="80"/>
            <GridViewColumn Header="Added" Width="80"/>
            <GridViewColumn Header="Automatically" Width="100"/>
        </GridView>
    </ListView.View>
</ListView>

My question is: can I define a number of ListView.View sections and switch between them depending of the object type in the list? The list will only contain one type of object at one time. I just need to display a different set of attributes.

The replies I have received so far seem to assume that I have a simple list which contains a mixture of objects. That is not the case. What I am looking for is something like this:

Name        Address      Town
-----------------------------------------
Liz         Buck House   London
Angie       Block House  Berlin

and then to switch to something completely different

Town       Population
------------------------------
London     123456
Swansea    65432

Perhaps I'm making this too complicated....

+1  A: 

First, define a DataTemplate

<DataTemplate x:Key="MyListTemplate">
    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
        <TextBlock FontSize="14" Foreground="Black" Name="tbNombre" Padding="5">
            <TextBlock.Text>
                <Binding Path="Nombre"></Binding>
            </TextBlock.Text>
        </TextBlock>
    </StackPanel>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=Active}"
                            Value="False">
            <Setter TargetName="tbNombre" Property="Foreground" Value="Silver" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

After that, change the data template dynamically:

Dim template As DataTemplate = Application.Current.Resources("MyListTemplate")
MyList.ItemTemplate = template
Eduardo Molteni
A: 

If you're selecting a template based on they type of the object then you can just set the DataType property of the template to that object. Then WPF will select the template that matches the type of the item.

<DataTemplate DataType="{x:Type l:MyType}"> ...

If you want to select a template based on some property within the type, a DataTemplateSelector is probably better.

See Bea Stollnitz's blog for more info.

Cameron MacFarland