具有相同视图的ListView

时间:2012-09-14 18:17:43

标签: c# wpf listview

我有一些ListView,我希望他们都有相同的View。因此,如果以下是我的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>

我想要这样的事情:

<Grid>

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

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

</Grid>

我该如何做到这一点?

2 个答案:

答案 0 :(得分:1)

在资源中定义视图(在顶级控件的资源字典中或在外部资源字典中):

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

答案 1 :(得分:0)

使用它。干杯

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