你說的對,DatePicker
控件沒有VisualStateManager
的焦點狀態。請注意,它是可能爲DatePicker
添加狀態組和聚焦/未聚焦狀態,但這不是最好的方法。
template對於DatePicker
控件包含DatePickerTextBox
control,根據MSDN,「代表DatePicker
的文本輸入」。
看看DatePickerTextBox
的模板,我們發現它有一個FocusStates
狀態組,以及Unfocused
和Focused
狀態的定義。我們還發現這樣一行:
<Border x:Name="FocusVisual" BorderBrush="#FF6DBDD1" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1" IsHitTestVisible="False" Opacity="0"/>
因此,大家可以看到,默認的「聚焦色」是#FF6DBDD1
。 DatePickerTextBox
的Focused
狀態將此邊框的Opacity
屬性設置爲1
。
要更改Focused
狀態的邊框顏色,可以創建此模板的副本,並用所需顏色替換#FF6DBDD1
。然後,創建DatePicker
模板的副本,該副本應指定其中包含的DatePickerTextBox
應使用修改後的模板。
或者,您可以創建DatePickerTextBox
模板的副本,使調整邊框的顏色,並把這個模板的風格與TargetType
設置爲DatePickerTextBox
:
<Style x:Key="MyStyle1" TargetType="DatePickerTextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DatePickerTextBox">
<!-- Modified template for DatePickerTextBox goes here -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
希望這是對你有用!
編輯:爲DatePickerTextBox
大部分的默認樣式和模板控件默認模板可供on MSDN。然而(無論出於何種原因),DatePickerTextBox
不是。如果你的Expression Blend的副本(我強烈建議,如果您正在使用的控件的外觀工作,它是無價的 - 下載免費試用here),你可以做到以下幾點:
在DatePicker
右擊,點擊「編輯模板 - >編輯副本...」。您會在「對象和時間線」面板中看到DatePickerTextBox
。右鍵單擊它,再次單擊「編輯模板 - >編輯副本...」。然後,您可以右鍵單擊「對象和時間線」面板中的模板,然後單擊「查看XAML」。
再一次,如果您正在做這樣的工作,我不能推薦Blend足夠高。從長遠來看,它將爲您節省大量時間(並且您將學習大量關於XAML,樣式,模板,它們如何融合在一起等)。如果你沒有訪問它,雖然,這裏是爲DatePickerTextBox
控件的默認模板:
<Style x:Key="DatePickerTextBoxStyle" TargetType="System_Windows_Controls_Primitives:DatePickerTextBox">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="System_Windows_Controls_Primitives:DatePickerTextBox">
<Grid x:Name="Root">
<Grid.Resources>
<SolidColorBrush x:Key="WatermarkBrush" Color="#FFAAAAAA"/>
</Grid.Resources>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0"/>
<VisualTransition GeneratedDuration="0:0:0.1" To="MouseOver"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="ContentElement">
<SplineColorKeyFrame KeyTime="0" Value="#FF99C1E2"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="ContentElement2">
<SplineColorKeyFrame KeyTime="0" Value="#FF99C1E2"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="WatermarkStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Unwatermarked"/>
<VisualState x:Name="Watermarked">
<Storyboard>
<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentElement"/>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Watermark"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Unfocused"/>
<VisualState x:Name="Focused">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisual"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1" Opacity="1">
<Grid x:Name="WatermarkContent" Background="{TemplateBinding Background}">
<Border x:Name="ContentElement" BorderBrush="#FFFFFFFF" BorderThickness="1" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"/>
<Border x:Name="ContentElement2" BorderBrush="#FFFFFFFF" BorderThickness="1">
<ContentControl x:Name="Watermark" Background="{TemplateBinding Background}" Content="{TemplateBinding Watermark}" Foreground="{StaticResource WatermarkBrush}" FontSize="{TemplateBinding FontSize}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="False" IsTabStop="False" Opacity="0" Padding="2" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<Border x:Name="FocusVisual" BorderBrush="#FF6DBDD1" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1" IsHitTestVisible="False" Opacity="0"/>
</Grid>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
道歉長度,非常感謝this thread(它有同樣的問題)。
您是否嘗試過建立在Expression Blend基礎上,DatePickerTextBox模板? – Ben 2010-12-14 01:19:09
@Ben,不,我不使用混合。 – Gabe 2010-12-14 15:09:30