开发者

'View cannot be shared by more than one ListView' System.Windows..ListView

开发者 https://www.devze.com 2023-01-25 08:37 出处:网络
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.

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>
..


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


Use DynamicResource rather than StaticResource.

See this question for full code.

0

精彩评论

暂无评论...
验证码 换一张
取 消