日期:2014-05-17  浏览次数:20806 次

WPF,请教一个Button模板的问题
在VS 2012中,往窗口拖了一个Button,然后右键编辑模板,Button的标准模板就出来了。看了下标准模板的内容,其中主要部分是这样的:

<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
    <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>


是由Border包装的ContentPresenter,我想问的是:
这里的Border并没有把Width和Height属性绑定到控件的对应属性上,但在改变Button的尺寸时,为什么Border跟着变化了呢?

------解决方案--------------------
button元素是放在ContentPresenter里面的,也就是说border是button的父元素的父元素,你没给boder设置的宽和高的话,默认的应该是auto,当子元素有宽高的值时父元素就自动撑开了
------解决方案--------------------
如果你觉得这个问题不好理解
去看一下“依赖属性”这个玩意 你就明白了

wpf的UI元素是依靠这个玩意来布局的
------解决方案--------------------
因为border的child改变了,那么它就改变了