WPF,模板的属性设置问题

东风6 2013-06-29 02:08:46
下面这个Button模板,来自MSDN的例子


<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>



我想问的是:
模板中的ContentPresenter,并没有绑定Foreground属性,为什么设置Button的Foreground属性,能起到作用呢?没有绑定的话,应该继承父元素的属性的啊
...全文
417 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
studentsky 2014-08-13
  • 打赏
  • 举报
回复
正解
引用 11 楼 ariesget 的回复:
[quote=引用 9 楼 u011247042 的回复:] [quote=引用 8 楼 ariesget 的回复:] 你要知道ContentPresenter的作用,它就相当于一个默认自动绑定使用模板的元素的Content内容的ContentControl,而你Button的Content相当于一个TextBlock,设置字体大小,颜色都会影响到的。
照你这么说,如果代码中没有绑定Background属性的句子,在Button上设置Background也会起作用的洛? [/quote] 不会,Button的默认Content只是个文字而已,会影响的也就字体跟字体颜色。其他比如长宽,背景都是通过模板绑定来实现的。 如果你改变了Button的Content,那么才可能受影响。 比如

        <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]
东风6 2013-07-01
  • 打赏
  • 举报
回复
引用 10 楼 MicrosoftCenterOfHN 的回复:
ContentPresenter内部做了默认的绑定. http://www.cnblogs.com/Clingingboy/archive/2010/12/20/1911388.html
ContentPresenter内部做了默认的绑定,只是对Content属性而言的吧,并没有设计Foreground属性的啊
ariesget 2013-07-01
  • 打赏
  • 举报
回复
引用 9 楼 u011247042 的回复:
[quote=引用 8 楼 ariesget 的回复:] 你要知道ContentPresenter的作用,它就相当于一个默认自动绑定使用模板的元素的Content内容的ContentControl,而你Button的Content相当于一个TextBlock,设置字体大小,颜色都会影响到的。
照你这么说,如果代码中没有绑定Background属性的句子,在Button上设置Background也会起作用的洛? [/quote] 不会,Button的默认Content只是个文字而已,会影响的也就字体跟字体颜色。其他比如长宽,背景都是通过模板绑定来实现的。 如果你改变了Button的Content,那么才可能受影响。 比如

        <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也会跟着设置的背景变化。
  • 打赏
  • 举报
回复
ContentPresenter内部做了默认的绑定. http://www.cnblogs.com/Clingingboy/archive/2010/12/20/1911388.html
东风6 2013-07-01
  • 打赏
  • 举报
回复
引用 8 楼 ariesget 的回复:
你要知道ContentPresenter的作用,它就相当于一个默认自动绑定使用模板的元素的Content内容的ContentControl,而你Button的Content相当于一个TextBlock,设置字体大小,颜色都会影响到的。
照你这么说,如果代码中没有绑定Background属性的句子,在Button上设置Background也会起作用的洛?
ariesget 2013-07-01
  • 打赏
  • 举报
回复
你要知道ContentPresenter的作用,它就相当于一个默认自动绑定使用模板的元素的Content内容的ContentControl,而你Button的Content相当于一个TextBlock,设置字体大小,颜色都会影响到的。
东风6 2013-07-01
  • 打赏
  • 举报
回复
有人知道么......
反对过分 2013-06-30
  • 打赏
  • 举报
回复
有人知道么......
东风6 2013-06-30
  • 打赏
  • 举报
回复
引用 4 楼 yuezaiziye 的回复:
你直接设置<Button Content="Button" Height="31" Foreground="Red> 这样肯定会有颜色啦。当模版没有绑定改变的时候,不代表你在button上不能改变Foreground颜色啊。
照你这么说,如果不绑定Background属性在,在Button上设置Background也会起作用的洛?
aggier 2013-06-29
  • 打赏
  • 举报
回复
你直接设置<Button Content="Button" Height="31" Foreground="Red> 这样肯定会有颜色啦。当模版没有绑定改变的时候,不代表你在button上不能改变Foreground颜色啊。
东风6 2013-06-29
  • 打赏
  • 举报
回复
有人知道没......
东风6 2013-06-29
  • 打赏
  • 举报
回复
引用 1 楼 sp1234 的回复:
父元素是Grid,父元素的父元素不就是Button嘛。
Grid的父元素是Button?不是Window?
  • 打赏
  • 举报
回复
父元素是Grid,父元素的父元素不就是Button嘛。

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧