基本上,我想覆盖,例如:TextBlock
超过Button
,通过使用ControlTemplate
(适用于本Button
),但我不希望摆脱违约它的模板。绑定ContentPresenter.Content到TemplatedParent(WPF/Silverlight的)
例子:
<Grid Grid.Row="1" Grid.ColumnSpan="2">
<Grid.Resources>
<Style x:Key="myStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<ContentPresenter />
<TextBlock Text="textBlock"
Margin="10" Foreground="Red"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Button Style="{StaticResource myStyle}" Content="button1"></Button>
</Grid>
,它给Button
剥夺了它的默认模板:
相反,我希望收到这样的事情:
使用ControlTemplate
可以吗?我试图绑定TemplatedParent
到ContentPresenter.Content
喜欢这里:
<ContentPresenter Content="{Binding
RelativeSource={RelativeSource Mode=TemplatedParent},
Path=.,
Mode=TwoWay}"/>
或其他组合,但我不能使它工作。
编辑:
因为我希望能够将此TextBlock
不仅适用于一个按钮(这只是一个例子),但任何Control
,我不想通过复制默认情况下做到这一点风格(对资源或某处),每Control
。 此外,我不希望创建UserControl
,因为我想尽可能保持xaml的清洁(我的意思是系统控制) - 并且只是通过使用ControlTemplate
打开/关闭重叠的TextBlock
。
谢谢你的回答,但那些opions会对我不好。我编辑了我的问题,使其更加精确。 – infografnet