Change DatePicker style based on SelectedDate
First question here. Anyway, here it goes:
I have a XAML Windows with a lot of DatePicker controls (DatePicker from the WPFToolkit on CodePlex). Every DatePicker has a default Value of 1/1/1990 and if no other date is selected, I want (or rather my boss :-) ) to display the Text in gray italic and not in Black. Thus, it makes it easy to see which fields the Date has not yet been entered.
This might be easy, but I'm quite new to WPF/开发者_开发问答XAML. In fact, these are my first steps using it.
So here is what I have and which works great actually:
<Style TargetType="{x:Type my:DatePicker}">
<Style.Triggers>
<Trigger Property="Text" Value="1/1/1990">
<Setter Property="Foreground" Value="DarkGray"/>
<Setter Property="ToolTip" Value="Please select a date"/>
<Setter Property="FontStyle" Value="Italic"/>
</Trigger>
</Style.Triggers>
</Style>
Problem is, it doesn't work on every machine because of localization/regional settings issues obviously.
So I tried this:
<Style TargetType="{x:Type my:DatePicker}">
<Style.Triggers>
<Trigger Property="Text" Value="{Binding Source={x:Static p:Settings.Default}, Path=MinDate, Mode=TwoWay}">
<Setter Property="Foreground" Value="DarkGray"/>
<Setter Property="ToolTip" Value="Veuillez choisir une date"/>
<Setter Property="FontStyle" Value="Italic"/>
</Trigger>
</Style.Triggers>
</Style>
Notice the difference in the "Value" property of the trigger. This yields following error:
A 'Binding' cannot be set on the 'Value' property of type 'Trigger'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
Which I do understand the meaning of and I understand why this doesn't work. (Note that MinDate is of type DateTime with Value 1/1/1990)
So, how could I achieve the result from my first code snippet on all computers?
Thanks for the time.
Ok, for those that happen to run into the same issue, here's what I finally did:
In XAML:
<src:ConvertMinDate x:Key="ConvertMinDate"/>
<Style TargetType="{x:Type my:DatePicker}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=SelectedDate, RelativeSource={RelativeSource Self},
Converter={StaticResource ConvertMinDate}}" Value="True">
<Setter Property="Foreground" Value="DarkGray"/>
<Setter Property="ToolTip" Value="Select a date"/>
<Setter Property="FontStyle" Value="Italic"/>
</DataTrigger>
</Style.Triggers>
</Style>
In codebehind:
public class ConvertMinDate : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
{
return true;
}
else
{
DateTime date = Peche.Properties.Settings.Default.MinDate;
if (value is DateTime)
{
date = (DateTime)value;
return date == Peche.Properties.Settings.Default.MinDate;
}
else
{
return true;
}
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
MinDate has been set to 1st of january 1990 in the Settings section of my project.
So now, if the SelectedDate is either null or equals MinDate, the converted return true, thus triggering the DataTrigger and changing the style if the DatePickerTextBox.
Isn't WPF just great? :-)
You don't want a trigger. You can just change the style of the template. This article is what helped me.
Also You can set the current day if you want. Here is a link to the documentation for Datepicker.
精彩评论