2015-04-16 87 views
3

怎么说,这ModernUI文本框风格完全改变

enter image description here

的外观改变这种

enter image description here

当所有我做的是添加此样式:

<UserControl.Resources> 
    <Style TargetType="{x:Type TextBox}"> 
     <Setter Property="TextAlignment" Value="Right"/> 
    </Style> 
</UserControl.Resources> 

当我设置属性可怕ctly的元素:

<TextBox Text="" TextAlignment="Right"></TextBox> 

一切看起来良好:

enter image description here

我真的不明白,并会在短期一丝感激。

回答

6

问题是它会完全取代其他风格。

添加BasedOn属性来指定,这种风格将延长ModernUI风格:

<UserControl.Resources> 
    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource whateverModernUIStyleNameIs}"> 
     <Setter Property="TextAlignment" Value="Right"/> 
    </Style> 
</UserControl.Resources> 

您也可以指定隐风格:BasedOn="{StaticResource {x:Type TextBox}}",由Mike C.

阅读这个提问时指出了解更多详情:How to apply multiple styles in WPF

+0

哦宕大卫创建一个样式,您可以通过从字面上秒打我笑+1只是为了那个! –

+1

感觉如此疯狂......如果我在编写时使用TargetType =“{x:Type TextBox}”和BasedOn =“{StaticResource {x:Type TextBox}}”来定义样式,那么它会按预期工作 – peter

+0

OP没有任何风格应用,而且是隐含的。您还应该包含一个示例,其中'BasedOn'基于隐式样式'{StaticResource {x:Type TextBox}}' – mclark1129

4

通过指定您正在设置TargetType的新样式模板TextBox您隐含地覆盖任何其他你已经为它设置了呃风格的模板。

如果您将BasedOn添加到您的模板中,以引用其他样式给它的红色边框,它将继承所有其他样式,因为它应该只会更改您的setter所属的属性。

所以<Style TargetType="{x:Type TextBox}">然后成为;

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource TheKeyNameOfTheOtherStyleTemplateYouAreReferencing}">

有意义吗?除了为什么你需要为它设置一个样式设置器,当你已经有一个绑定到模板的属性来完成你想要的一个依赖项属性?

3

因为你替换现有的样式与一个只设置文本对齐方式。

Resourcesbased on的默认样式

<Style 
    BasedOn="{StaticResource {x:Type TextBox}}" 
    TargetType="{x:Type TextBox}"> 
    <Setter Property="TextAlignment" Value="Right"/> 
</Style>