views:

1579

answers:

2

Hi all, 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.

A: 

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.

nportelli
Thanks for the links. However, I don't actually want to change the style of the Calendar control. Just of the Text displayed in the DatePickerTextBox. I use the trigger, because I want to change the style of the textBox based on the value of it. I might not have been clear about that yesterday. If the Text property of the DatePickerTextBox is 01/01/1990, I want the foreground to be DarkGray Italic, otherwise change back to Normal Black, hence having a visual feedback what fields have the default value. That's the reason I figured using a trigger might just be the right thing.
Gilles Radrizzi
Ok you may want a trigger, I just don't think using that date is the best thing to base it off of. I'd probably bind to a bool property so you didn't have to worry about localization.
nportelli
Thanks, that helped me get on the right track. I now created a converter which return true if the SelectedDate property is equal to my default Date value, thus triggering the style change. Works like a charm. Thanks again.
Gilles Radrizzi
A: 

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? :-)

Gilles Radrizzi