2010-12-07 162 views
1

我想更改DatePicker控件上焦點狀態的邊框顏色。 我看了一下默認樣式模板,沒有看到VisualStateManager的焦點狀態。更改DatePicker的邊框顏色

我看到的唯一的事情是爲TextBox原始控制如下:

<controlsPrimitives:DatePickerTextBox x:Name="TextBox" SelectionBackground="{TemplateBinding SelectionBackground}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Grid.Column="0" /> 

我怎樣才能更改焦點狀態的顏色邊框爲DatePicker ...我沒有問題,改變此顏色爲TextBox,ComboBoxCheckBox控件。

請幫忙!

+0

您是否嘗試過建立在Expression Blend基礎上,DatePickerTextBox模板? – Ben 2010-12-14 01:19:09

+0

@Ben,不,我不使用混合。 – Gabe 2010-12-14 15:09:30

回答

4

你說的對,DatePicker控件沒有VisualStateManager的焦點狀態。請注意,它可能爲DatePicker添加狀態組和聚焦/未聚焦狀態,但這不是最好的方法。

template對於DatePicker控件包含DatePickerTextBox control,根據MSDN,「代表DatePicker的文本輸入」。

看看DatePickerTextBox的模板,我們發現它有一個FocusStates狀態組,以及UnfocusedFocused狀態的定義。我們還發現這樣一行:

<Border x:Name="FocusVisual" BorderBrush="#FF6DBDD1" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1" IsHitTestVisible="False" Opacity="0"/> 

因此,大家可以看到,默認的「聚焦色」是#FF6DBDD1DatePickerTextBoxFocused狀態將此邊框的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(它有同樣的問題)。

0

爲你做這項工作:

public class MyDatePicker : DatePicker 
{ 
    public MyDatePicker() 
    { } 

    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 

     DatePickerTextBox textBox = (DatePickerTextBox)this.GetTemplateChild("TextBox"); 
     textBox.GotFocus += new RoutedEventHandler(textBox_GotFocus); 
     textBox.LostFocus += new RoutedEventHandler(textBox_LostFocus); 
    } 

    void textBox_GotFocus(object sender, RoutedEventArgs e) 
    { 
     (sender as DatePickerTextBox).BorderBrush = new SolidColorBrush(Colors.Red); 
    } 

    void textBox_LostFocus(object sender, RoutedEventArgs e) 
    { 
     (sender as DatePickerTextBox).ClearValue(DatePickerTextBox.BorderBrushProperty); 
    } 
}