2015-04-16 85 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>