我目前有一个包含3个网格和一个带有相同网格的滚动查看器的透视图。像任何软件工程师一样,我想只编写一次代码而不是两次。因此我的问题是:如何实现这一点?
我目前有一个包含3个网格和一个带有相同网格的滚动查看器的透视图。像任何软件工程师一样,我想只编写一次代码而不是两次。因此我的问题是:如何实现这一点?
我找到了解决方案:我将我的三个网格放在三个单独的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>