110,535
社区成员
发帖
与我相关
我的任务
分享
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="RootElement">
<Border.Background>
<SolidColorBrush x:Name="BorderBrush" Color="Black"/>
</Border.Background>
<Grid Margin="4" Background="{TemplateBinding Background}">
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="4,5,4,4" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid Height="100" Name="grid1" Width="200">
<Button Content="Button" Height="31" Foreground="Red" HorizontalAlignment="Left" Margin="60,45,0,0" Name="button1" VerticalAlignment="Top" Width="78" />
</Grid>
<Button Width="100" Height="30" Background="Gray" Foreground="Red">
<Grid Width="{Binding Width,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}}" Background="{Binding Background,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}}" Height="{Binding Height,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}}">
<TextBlock Text="test"/>
</Grid>
</Button>
这个的Content跟父元素的背景绑定了。这样模板没绑定背景的情况下,只用ContentPresenter也会跟着设置的背景变化。[/quote]
<Button Width="100" Height="30" Background="Gray" Foreground="Red">
<Grid Width="{Binding Width,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}}" Background="{Binding Background,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}}" Height="{Binding Height,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}}">
<TextBlock Text="test"/>
</Grid>
</Button>
这个的Content跟父元素的背景绑定了。这样模板没绑定背景的情况下,只用ContentPresenter也会跟着设置的背景变化。