我有一些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>


我该怎么做?

最佳答案

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

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

关于c# - 具有相同 View 的ListView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12429878/

10-09 09:43