2012-11-08 140 views
2

基本上,我想覆盖,例如: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剥夺了它的默认模板:

enter image description here

相反,我希望收到这样的事情:

enter image description here

使用ControlTemplate可以吗?我试图绑定TemplatedParentContentPresenter.Content喜欢这里:

<ContentPresenter Content="{Binding 
     RelativeSource={RelativeSource Mode=TemplatedParent}, 
     Path=., 
     Mode=TwoWay}"/> 

或其他组合,但我不能使它工作。

编辑:

因为我希望能够将此TextBlock不仅适用于一个按钮(这只是一个例子),但任何Control,我不想通过复制默认情况下做到这一点风格(对资源或某处),每Control。 此外,我不希望创建UserControl,因为我想尽可能保持xaml的清洁(我的意思是系统控制) - 并且只是通过使用ControlTemplate打开/关闭重叠的TextBlock

回答

1

您可以在按钮上添加default style并修改它以添加您的TextBlock。第二个选项,我的偏好是创建一个新的用户控件,其中将包含ButtonTextBlock以及IsHitTestVisible=False。然后,您可以添加依赖项属性以便能够绑定到按钮和文本块。

+0

谢谢你的回答,但那些opions会对我不好。我编辑了我的问题,使其更加精确。 – infografnet