12

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>

2
  • Could you post a portion of your code? Mar 28, 2010 at 19:11
  • Can you show us your style please ? Mar 29, 2010 at 14:36

1 Answer 1

23

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 Margin="0,0,0,120"    View="{DynamicResource ViewBase1}" />
    <ListView Margin="272,0,91,120" View="{DynamicResource ViewBase1}" />
  </Grid>
</Window>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.