首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

WPF,属性继承有关问题

2013-07-11 
WPF,属性继承问题Background{TemplateBinding Background}Padding{TemplateBinding Padding}SnapsTo

WPF,属性继承问题


                        Background="{TemplateBinding Background}"
                        Padding="{TemplateBinding Padding}"
                        SnapsToDevicePixels="True">
                    <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
                                        Content="{TemplateBinding Content}"
                                        ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                        RecognizesAccessKey="True"
                                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled"
                                Value="False">


                        <Setter Property="Foreground"
                                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>



DynamicResource {x:Static SystemColors.ControlTextBrushKey}
[解决办法]
<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Foreground="Red">
    <StackPanel >
        <Label Content="熊俊" />    <TextBlock Text="12334" />
    </StackPanel>

</Window>


TextBlock 是有效果的哦。。。

MSDN上是这么写的
This property only affects a control whose template uses the Foreground property as a parameter. On other controls, this property has no impact.


所以对TextBlock可以起作用而CheckBox, Button一类的就不起作用。

我们在使用Button的Foreground属性时,实际上也只是对Button内部的TextBlock的Foreground产生了作用,用Snoop查看Button的VisualTree可以发现
Button
  Chrome
    ContentPresenter
      TextBlock

也就是Button的内容(字符串)实际上是在TextBlock中被显示出来,并且Foreground属性也是被应用到了TextBlock上。

所以LZ还是一个一个设置吧~~~
[解决办法]
如果你想用的话 TextBlock  也有lable的效果
[解决办法]
<TextBlock.Foreground>  
                    <LinearGradientBrush>   


                            <GradientStop Color="Green"></GradientStop>  
                            <GradientStop x:Name="gcc1"  Color="Green" Offset="0.3"></GradientStop>  
                            <GradientStop x:Name="gcc2" Color="Blue" Offset="0.3"></GradientStop>  
                            <GradientStop Color="Blue" Offset="1"></GradientStop>   
                    </LinearGradientBrush>  
                </TextBlock.Foreground>  
你看看这段代码,希望对你有帮助,这是设置的TextBlock的属性颜色,改成Lable就行了
[解决办法]
这东西自己不支持继承的话,你改模板也不行,除非设置个依赖项属性进行统一绑定,那样也不麻烦。
[解决办法]
 <StackPanel > 
        <Label Content="熊俊" Foreground="Red" /> 
    </StackPanel> 

热点排行