Search code examples
datepickerwindows-runtimewindows-phonewindows-phone-8.1foreground

Datepicker - disabled foreground color Windows Phone 8.1 RT


How to set the foreground of DatePicker for disabled state?

I have try set style res:

<SolidColorBrush x:Key="DatePickerForegroundThemeBrush" Color="#444444" />

and:

dpDate.IsEnabled = false; dpDate.Foreground = new SolidColorBrush(Colors.Black);

but it still transparent.


Solution

  • You need to modify template of the DatePicker control in order to change it's behavior when it is in disabled visual state; actually, there is a button inside the DatePicker and you need to change behavior of that button.

    1. In designer, right click on the DatePicker and then select Edit Template -> Edit a copy -> OK. Now designer has generated an style for the control.
    2. Perform step one again to generate a style for the button inside the date picker.
    3. Go to XAML view. There are two style elements under the Page.Resources element: ButtonStyle1 and DatePickerStyle1. Find and comment the following part:

    XAML:

    <VisualState x:Name="Disabled">
        <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground"
                                           Storyboard.TargetName="ContentPresenter">
                <DiscreteObjectKeyFrame KeyTime="0"
                                        Value="{ThemeResource ButtonDisabledForegroundThemeBrush}" />
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush"
                                           Storyboard.TargetName="Border">
                <DiscreteObjectKeyFrame KeyTime="0"
                                        Value="{ThemeResource ButtonDisabledBorderThemeBrush}" />
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background"
                                           Storyboard.TargetName="Border">
                <DiscreteObjectKeyFrame KeyTime="0"
                                        Value="{ThemeResource ButtonDisabledBackgroundThemeBrush}" />
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </VisualState>
    

    For a more explained example on customizing control templates, checkout Quickstart: Control templates (XAML).