1

I'm trying to set the View for my ListView dynamically: But I get an Exception 'View cannot be shared by more than one ListView' although I'm using it only once. Anyways if I make an instance already, and then the DataTrigger triggers, the Exception takes place too.

This is my wpf code:

<ListView ItemsSource="{Binding Collection}" SelectionMode="Extended" AlternationCount="2" >
    <ListView.Style>
        <Style>
            <Setter Property="ListView.View" Value="{StaticResource MyView1}" />
            <Style.Triggers>                
                <DataTrigger Binding="{Binding Path=MyPath1}" Value="True">
                    <Setter Property="ListView.View" Value="{StaticResource MyView2}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=MyPath2}" Value="True">
                    <Setter Property="ListView.View" Value="{StaticResource MyView3}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListView.Style>
</ListView>

and this is from Resources:

<GridView x:Key="MyView1">
    <GridViewColumn DisplayMemberBinding="{Binding Path=MyProperty}" >
        <GridViewColumnHeader Content="Text" />
    </GridViewColumn>
</GridView>
..
theSpyCry
  • 12,073
  • 28
  • 96
  • 152

2 Answers2

7

You can add the x:Shared="False" attribute to your GridView resources like this

<GridView x:Key="MyView1" x:Shared="False">
    <GridViewColumn DisplayMemberBinding="{Binding Path=MyProperty}" > 
        <GridViewColumnHeader Content="Text" /> 
    </GridViewColumn> 
</GridView> 
.. 

Update

Uploaded sample project here

Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
6

Use DynamicResource rather than StaticResource.

See this question for full code.

Community
  • 1
  • 1
Grammarian
  • 6,774
  • 1
  • 18
  • 32
  • This works for me too.. 2 conditions: DynamicResource + initial View definition - trigger all templates with DataTrigger – theSpyCry Nov 16 '10 at 14:57