如何在UWP Windows 10中重复使用XAML中的网格?

3

我目前有一个包含3个网格和一个带有相同网格的滚动查看器的透视图。像任何软件工程师一样,我想只编写一次代码而不是两次。因此我的问题是:如何实现这一点?


一个例子会很有帮助,因为我找不到一个合适的。 - Teysz
请查看以下链接:http://www.codeproject.com/Articles/32825/How-to-Creating-a-WPF-User-Control-using-it-in-a-W - Akansha
1
这些是WPF示例,我正在寻找标题中提到的UWP示例(Windows 10)。 - Teysz
XAML 在不同平台上有90%的相似度,所以请放心使用 WPF 的例子 :) - Tamás Deme
你能更具体地说明“重用”吗?你想要重用属性设置还是其他什么?有几种方法可以进行重用,你需要根据想要重用的部分选择适当的方法。 - Jeffrey Chen
@JeffreyChen-MSFT,我有3个网格,我想只使用一次。但是我已经解决了 :) - Teysz
1个回答

5

我找到了解决方案:我将我的三个网格放在三个单独的DataTemplates中,并从Pivot和ScrollViewer内部引用这些模板:

<Page.Resources>
   ...
   <DataTemplate x:Key="JustANormalGridNr1">
      <Grid />
   </DataTemplate>
   <DataTemplate x:Key="JustANormalGridNr2">
      <Grid />
   </DataTemplate>
   <DataTemplate x:Key="JustANormalGridNr3">
      <Grid />
   </DataTemplate>
</Page.Resources>

<Grid x:Name="MasterGrid">
   <Pivot>
      <Pivot.Items>
         <PivotItem>
            ...
            <Grid>
               <ContentControl ContentTemplate="{StaticResource JustANormalGridNr1}" /><!--instead of the grid, a reference to it -->
            </Grid>
         </PivotItem>
         <PivotItem>
            ...
            <Grid>
               <ContentControl ContentTemplate="{StaticResource JustANormalGridNr2}" /><!--instead of the grid, a reference to it -->
            </Grid>
         </PivotItem>
         <PivotItem>
            ...
            <Grid>
               <ContentControl ContentTemplate="{StaticResource JustANormalGridNr3}" /><!--instead of the grid, a reference to it -->
            </Grid>
         </PivotItem>
      </Pivot.Items>
   </Pivot>

   <ScrollViewer>
      <Grid>
         <Grid>
            ...
            <Grid Grid.Column="0">
               ...
               <ContentControl Grid.Row="1" ContentTemplate="{StaticResource JustANormalGridNr1}" /><!-- Instead of the grid, a reference to it -->
            </Grid>
            <Grid Grid.Column="1">
               ...
               <ContentControl Grid.Row="1" ContentTemplate="{StaticResource JustANormalGridNr2}" /><!-- Instead of the grid, a reference to it -->
            </Grid>
            <Grid Grid.Column="2">
               ...
               <ContentControl Grid.Row="1" ContentTemplate="{StaticResource JustANormalGridNr3}" /><!-- Instead of the grid, a reference to it -->
            </Grid>
         </Grid>
      </Grid>
   </ScrollViewer>
</Grid>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接