gliderkite
gliderkite

Reputation: 8928

ListViews with the same View

I have some ListView and I want that they all have the same View. So, if the following is my View:

<ListView.View>
    <GridView>
      <GridViewColumn Width="140" Header="Game Name" 
         DisplayMemberBinding="{Binding GameName}"  />
      <GridViewColumn Width="140" Header="Creator"  
         DisplayMemberBinding="{Binding Creator}" />
      <GridViewColumn Width="140" Header="Publisher" 
         DisplayMemberBinding="{Binding Publisher}" />
    </GridView>
</ListView.View>

I want something like this:

<Grid>

 <ListView>
   <!--Here my ListView is the same defined above-->
 </ListView>

 <ListView>
   <!--Here my ListView is the same defined above-->
 </ListView>

</Grid>

How can I accomplish this?

Upvotes: 2

Views: 52

Answers (2)

Rikki
Rikki

Reputation: 3528

Use this. Cheers

<Grid>

    <ListView Name="FirstList">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="140" Header="Game Name" DisplayMemberBinding="{Binding GameName}"  />
                <GridViewColumn Width="140" Header="Creator" DisplayMemberBinding="{Binding Creator}" />
                <GridViewColumn Width="140" Header="Publisher" DisplayMemberBinding="{Binding Publisher}" />
            </GridView>
        </ListView.View>
    </ListView>

    <ListView View="{Binding Path=View, ElementName=FirstList}" />

</Grid>

Upvotes: 0

Dennis
Dennis

Reputation: 37770

Define view in resources (either in resource dictionary of top-level control, or in external resource dictionary):

<StackPanel>
    <StackPanel.Resources>
        <GridView x:Key="myView" x:Shared="false">
            <GridViewColumn Width="140" Header="Game Name" 
     DisplayMemberBinding="{Binding GameName}"  />
            <GridViewColumn Width="140" Header="Creator"  
     DisplayMemberBinding="{Binding Creator}" />
            <GridViewColumn Width="140" Header="Publisher" 
     DisplayMemberBinding="{Binding Publisher}" />
        </GridView>
    </StackPanel.Resources>

    <ListView View="{StaticResource myView}"/>
    <ListView View="{StaticResource myView}"/>
</StackPanel>

Upvotes: 1

Related Questions