Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the same style in many listviews

Tags:

wpf

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>

like image 587
ErikTJ Avatar asked Mar 28 '10 12:03

ErikTJ


1 Answers

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>
like image 187
CodeWarrior Avatar answered Nov 08 '22 22:11

CodeWarrior