[英]Style for Textbox Silverlight
我想為文本框制作一個簡單的樣式。 除了一個項目,我想保留標准文本框外觀的所有內容。
OnFocus on想要能夠改變文本框的邊框顏色。
我寫了以下內容,確實有效。 然而,一切都重新設計,我必須聲明高度,非聚焦邊界的外觀和感覺也是不同的。 如何創建模板以僅影響onfocus狀態。
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<Setter Property="BorderBrush" Value="Gold" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid Height="{TemplateBinding Height}"
>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Pressed" />
<VisualState x:Name="Disabled" />
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd"
Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
Duration="0"
To="Red" />
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="brd"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
CornerRadius="2">
<ContentPresenter x:Name="contentPresenter" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
您需要復制原始TextBox的整個模板,您可以在此處找到它。 然后進行所需的更改。
SirDemon提到的例子......
這是一個文本塊的樣式:
<Style
x:Key="detailBlk"
TargetType="TextBlock">
<Setter
Property="FontSize"
Value="10" />
<Setter
Property="Foreground"
Value="Purple" />
</Style>
假設我想要另一種FontSize為20的樣式,但前景色仍然是紫色:
<Style
x:Key="detailBlk20"
TargetType="TextBlock"
BasedOn="{StaticResource detailBlk}">
<Setter
Property="FontSize"
Value="20" />
</Style>
編輯:對不起,重讀問題。 您想要更改模板。 setter屬性可以設置任何屬性。 有趣的是,模板是一個屬性,因此可以在樣式中設置。 但是,據我所知,您無法更改模板的各個部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.