tags:

views:

42

answers:

1

I want to use the same style in many listviews. And in my style i have defined the gridview columns also.

But when i try to run, it throws an exception:

View can't be shared by more than one ListView.

How can i solve this?


XAML:

        <Style x:Key="articleList" TargetType="{x:Type ListView}">
        <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
        <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="True"/>
        <Setter Property="ListView.ItemsSource" Value="{Binding}"/>
        <Setter Property="ListView.View">
            <Setter.Value>
                <GridView>
                    <GridViewColumn Header="Subject" Width="300">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Subject}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Size" Width="75">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding SizeFormatted}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Poster" Width="175">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Poster}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Age" Width="75">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding AgeFormatted}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </Setter.Value>
        </Setter>
    </Style>
+1  A: 

Add the x:shared property to your GridView resource. check out the GridView resource in this example.

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Window1"
x:Name="Window"
Title="Window1"
Width="640" Height="480">
<Window.Resources>

    <GridView x:Key="ViewBase1" x:Shared="False">
        <GridViewColumn Header="Blah1" Width="70"/>
        <GridViewColumn Header="Blah2" Width="70"/>
        <GridViewColumn Header="Blah3" Width="70"/>
    </GridView>
</Window.Resources>

<Grid x:Name="LayoutRoot">
    <ListView HorizontalAlignment="Left" Width="272" Margin="0,0,0,120" View="{DynamicResource ViewBase1}"/>
    <ListView Margin="272,0,91,120" View="{DynamicResource ViewBase1}">
    </ListView>
</Grid>

Cory

OffApps Cory