Make DatePicker easier to read when disabled

Text is disabled when DatePicker is disabled, and I want the content to be easier to read.

What I did in some text blocks:

<Style TargetType="TextBox">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="Black" />
            </Trigger>
        </Style.Triggers>
</Style>

This made it easier to read the text.

I manage to change the foreground color to a DataPicker, but that doesn't do the trick. The text was still grayed out.

There seems to be another property that I need to configure to make it easier to read the contents of a disabled DatePicker.

So, how to make it easier to read the contents of my disabled DatePicker?

+3
source share
3 answers

DatePicker, bool DependencyProperty Editable.
, , .NET 4.

DatePicker:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;

namespace DatePickerStyle
{
 public class ExtendedDatePicker : DatePicker
 {
    public static readonly DependencyProperty EditableProperty = DependencyProperty.Register("Editable", typeof(bool),
        typeof(ExtendedDatePicker), new PropertyMetadata(true));
    public bool Editable
    {
       get { return (bool)GetValue(EditableProperty); }
       set { SetValue(EditableProperty, value); }
    }

    public override void OnApplyTemplate()
    {
       base.OnApplyTemplate();
       var textBox = GetTemplateChild("PART_TextBox") as DatePickerTextBox;
       var binding = new Binding { Source = this, Path = new PropertyPath(ExtendedDatePicker.EditableProperty) };
       textBox.SetBinding(UIElement.FocusableProperty, binding);
    }
  }
}

XAML:

<Window x:Class="DatePickerStyle.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:DatePickerStyle="clr-namespace:DatePickerStyle" 
    Title="MainWindow" Height="350" Width="525">
  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <DatePicker IsEnabled="True" Grid.Row="0" SelectedDate="2002/12/31"/>
    <DatePicker IsEnabled="False" Grid.Row="1" SelectedDate="2002/12/31"/>
    <DatePickerStyle:ExtendedDatePicker Editable="True" Grid.Row="2" SelectedDate="2002/12/31"/>
    <DatePickerStyle:ExtendedDatePicker Editable="False" Grid.Row="3" SelectedDate="2002/12/31"/>
  </Grid>
</Window>
+3

, ?

<ControlTemplate x:Key="MyDisabledDatePicker">
    <Border BorderBrush="Black" BorderThickness="1">
        <TextBlock 
            Text="{Binding Path=SelectedDate, StringFormat={}{0:d}, RelativeSource={RelativeSource TemplatedParent}}" 
            VerticalAlignment="Center" HorizontalAlignment="Left" Padding="10,0,0,0"/>
    </Border>
</ControlTemplate>
<Style TargetType="{x:Type DatePicker}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}}"  Value="false">
            <Setter Property="Template" Value="{StaticResource MyDisabledDatePicker}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Presto!

+2

w ExtendedDatePicker , , , , , Editable = "False"

ExtendedDatePicker :

Editable="False" AllowDrop="False" IsDropDownOpen="False" IsHitTestVisible="False" IsManipulationEnabled="False"
+1

Source: https://habr.com/ru/post/1767318/


All Articles