在我的WPF应用程序中,我想重复使用几次网格模板。 我为网格定义了一个数据模板(名为GrdTemplate),我想在我的XAML定义的几个地方使用这个模板。 如何使用网格模板?
这是我的XAML
代码:
<Grid Height="{Binding Converter={StaticResource PercentageConverter}, ElementName=listboxItems, Path=ActualHeight, ConverterParameter=0.48}"
MaxWidth="{Binding Converter={StaticResource PercentageConverter}, ElementName=listboxItems, Path=ActualWidth, ConverterParameter=0.1}">
<Grid.Resources>
<Style TargetType="TextBlock" >
<Setter Property="TextAlignment" Value="Center" />
<Setter Property="Margin" Value="2,2" />
</Style>
<DataTemplate x:Key="GrdTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Grid.Row="0">
<Grid.RowDefinitions >
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="8*" />
<ColumnDefinition Width="5*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0"
Text="{Binding Path=Tr}" />
<TextBlock Grid.Column="1" Grid.Row="0"
Text="{Binding Path=Hr}" />
</Grid>
<Grid Grid.Column="0" Grid.Row="1">
<Grid.RowDefinitions >
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0"
Text="{Binding Path=TypeK}" />
</Grid>
<Grid Grid.Column="0" Grid.Row="2">
<Grid.RowDefinitions >
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6*" />
<ColumnDefinition Width="6*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0"
Text="{Binding Path=Tk}" />
<TextBlock Grid.Row="0" Grid.Column="1"
Text="{Binding Path=Lft}" />
</Grid>
<Grid Grid.Column="0" Grid.Row="3">
<Grid.RowDefinitions >
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0"
Text="{Binding Path=Crd}" />
</Grid>
</Grid>
</DataTemplate>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="3*" />
<RowDefinition Height="4*" />
<RowDefinition Height="4*" />
</Grid.RowDefinitions>
<Border Grid.Column="0" Grid.Row="1"
Background="#FF576577"
BorderBrush="{DynamicResource GrayBrush7}" BorderThickness="2">
<Viewbox Stretch="Uniform" >
!!! Here I want to use my template with Object1 as Datasource !!!
</Viewbox>
</Border>
<Border Grid.Column="0" Grid.Row="1"
Background="#FF576577"
BorderBrush="{DynamicResource GrayBrush7}" BorderThickness="2">
<Viewbox Stretch="Uniform" >
!!! Here I want to use my template with Object2 as Datasource !!!
</Viewbox>
</Border>
</Grid>
答案 0 :(得分:1)
这里应该使用的不是DataTemplate
而是UserControl
。 DataTemplates
通常用于具有您希望采用相同外观的子控件集合的控件。
然后使用您的自定义UserControl:
<Viewbox Stretch="Uniform">
<!--Here I want to use my template with Object2 as Datasource-->
<views:MyGrdUserControl DataContext="{Binding Object2}"/>
</Viewbox>
如果您确实想使用DataTemplate
,可以使用ContentPresenter
并将ContentTemplate
设置为您的GrdTemplate资源
<Viewbox Stretch="Uniform">
<!--Here I want to use my template with Object2 as Datasource-->
<ContentPresenter Content="{Binding Object2}"
ContentTemplate="{StaticResource GrdTemplate}"/>
</Viewbox>