我有一个数据模板是:
<DataTemplate x:Key="CalDescButton">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="0"
Grid.ColumnSpan="2"
Style="{StaticResource LargeIconStyle}"
Text="" />
<TextBlock
Grid.Row="0"
Grid.Column="1"
Foreground="Transparent"
Style="{StaticResource MediumIconStyle}"
Text="" />
<TextBlock
Grid.Row="1"
Grid.Column="1"
Foreground="Transparent"
Style="{StaticResource MediumIconStyle}"
Text="" />
<TextBlock
Grid.Row="0"
Grid.Column="1"
Foreground="Transparent"
Style="{StaticResource MediumIconStyle}"
Text="" />
<TextBlock
Grid.Row="1"
Grid.Column="0"
Foreground="Transparent"
Style="{StaticResource MediumIconStyle}"
Text="" />
<TextBlock
Grid.Row="1"
Grid.RowSpan="2"
Grid.Column="0"
Margin="0,-6,0,0"
Foreground="Aqua"
Style="{StaticResource XSmallIconStyle}"
Text="" />
</Grid>
</DataTemplate>
然后我还有其他 6 个,唯一改变的是文本字符代码。 这些模板应用于按钮:
<ToggleButton
Command="{Binding CmdSetSorting, Mode=OneWay}"
CommandParameter="CalAsc"
ContentTemplate="{StaticResource CalDescButton}" />
问题是:我可以将变量传递给模板并在 Text=" 中使用它,而不是为此使用 6 个不同的模板吗? 我该怎么做?
为了清楚起见,我想要这样的东西:
<DataTemplate x:Key="IconButton">
然后:
ContentTemplate="{StaticResource IconButton ()}" />
设置按钮的内容属性:
<ToggleButton
Command="{Binding CmdSetSorting, Mode=OneWay}"
CommandParameter="CalAsc"
Content=""
ContentTemplate="{StaticResource CalDescButton}" />
它应该成为 DataTemplate 中根元素的 DataContext(因为它是 ContentTemplate(,并且可以通过 Binding 访问:
<TextBlock
Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="0"
Grid.ColumnSpan="2"
Style="{StaticResource LargeIconStyle}"
Text="{Binding}" />