This project is read-only.

Event SelectedDateChanged doesn't work

Nov 17, 2008 at 1:29 PM
Hello,

I've use WPF ToolKit in a ressource dictionary, and the selectedDate is always null

Here is the code from controls.generic.xaml :

<!-- labeled DatePicker -->

    <Style TargetType="{x:Type Controls:LabeledDatePickerControl}">
        <Setter Property="Validation.ErrorTemplate" Value="{StaticResource LabeledValidationTemplate}"/>
        <Setter Property="Width" Value="300"/>
        <Setter Property="Height" Value="24"/>
        <Setter Property="Margin" Value="8,8,8,0"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="FontFamily" Value="Verdana"/>
        <Setter Property="FontSize" Value="9"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Foreground" Value="{StaticResource ControlForegroundBrush}"/>

        <Setter Property="Template">
            <Setter.Value>

                <ControlTemplate TargetType="Controls:LabeledDatePickerControl">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="20"/>
                            <ColumnDefinition Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:LabeledDatePickerControl}}, Path=LabelWidth}"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <TextBlock x:Name="MandatoryIndicatorControl"
                                   Foreground="{StaticResource MandatoryIndicatorForegroundBrush}"
                                   VerticalAlignment="Center"
                                   TextBlock.FontWeight="Bold"
                                   Grid.Column="0"
                                   Text="*"
                                   Visibility="Hidden"
                                   />

                        <TextBlock  Grid.Column="1"
                                    VerticalAlignment="Center"
                                    Foreground="{StaticResource LabelForegroundBrush}"
                                    Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:LabeledDatePickerControl}}, Path=LabelText}"
                                    />

                        <toolkit:DatePicker x:Name="DtPicker"
                                            Height="24" Grid.Column="2"
                                            Foreground="{StaticResource ControlForegroundBrush}"
                                            Background="{StaticResource ControlBackgroundBrush}"
                                            BorderBrush="{StaticResource ControlBorderBrush}"
                                            VerticalAlignment="Bottom"
                                            VerticalContentAlignment="Center"
                                            FontFamily="Verdana" FontSize="10" />

                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="DtPicker" Property="Background"
                                    Value="{StaticResource DisabledBackgroundBrush}" />
                            <!--<Setter TargetName="DtPicker" Property="IsEnabled"
                                    Value="False" />-->
                            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                        </Trigger>

                        <Trigger Property="Validation.HasError" Value="True">
                            <Setter Property="ToolTip"
                                    Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}"/>
                        </Trigger>

                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMandatory}"
                                     Value="True">
                            <Setter TargetName="MandatoryIndicatorControl" Property="Visibility" Value="Visible"/>
                        </DataTrigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>

            </Setter.Value>
        </Setter>
    </Style>

And the code from file LabeledDatePickerControl.cs

using System;
using System.Windows;
//using System.Windows.Data;
using Microsoft.Windows.Controls;

namespace FBL.Registre.Core.UI.Controls
{
    public class LabeledDatePickerControl : DatePicker
    {
        public static readonly DependencyProperty LabelTextProperty = DependencyProperty.Register("LabelText", typeof(string), typeof(LabeledDatePickerControl), new PropertyMetadata(""));
        public static readonly DependencyProperty LabelWidthProperty = DependencyProperty.Register("LabelWidth", typeof(string), typeof(LabeledDatePickerControl), new PropertyMetadata("100"));
        public static readonly DependencyProperty IsMandatoryProperty = DependencyProperty.Register("IsMandatory", typeof(Boolean), typeof(LabeledDatePickerControl), new PropertyMetadata(false));
        public static readonly DependencyProperty ValidationMarginProperty = DependencyProperty.Register("ValidationMargin", typeof(string), typeof(LabeledDatePickerControl), new PropertyMetadata("60,0,0,0"));

        static LabeledDatePickerControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(LabeledDatePickerControl), new FrameworkPropertyMetadata(typeof(LabeledDatePickerControl)));
        }

        public string LabelText
        {
            get { return (string)GetValue(LabelTextProperty); }
            set { SetValue(LabelTextProperty, value); }
        }

        public string LabelWidth
        {
            get { return (string)GetValue(LabelWidthProperty); }
            set { SetValue(LabelWidthProperty, value); }
        }

        public Boolean IsMandatory
        {
            get { return (Boolean)GetValue(IsMandatoryProperty); }
            set { SetValue(IsMandatoryProperty, value); }
        }

        public string ValidationMargin
        {
            get { return (string)GetValue(ValidationMarginProperty); }
            set { SetValue(ValidationMarginProperty, value); }
        }

        public string CheckValidation()
        {
            string validationMessage = "";

            if (IsMandatory)
            {
                if (SelectedDate == null)
                    validationMessage += LabelText + " champ obligatoire.";
            }

            return validationMessage;
        }
    }
}
In the xaml page, I've this :

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2006"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
             xmlns:Controls="clr-namespace:FBL.Registre.Core.UI.Controls;assembly=FBL.Registre.Core.UI.Controls"
             xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
             x:Class="FBL.Registre.Processing.UI.StatsMS"
             >

    <StackPanel>

        <Controls:LabelControl  x:Name="errorLabel"
                                HorizontalAlignment="Right" VerticalAlignment="Top"
                                Width="800" MaxHeight="50"
                                Content="" Foreground="Red" Margin="0,8,0,0"
                                TextBlock.TextAlignment="Right"
                                Block.TextAlignment="Right" />

        <Controls:LabelControl x:Name="ConfirmStart"
                               HorizontalAlignment="Left"  VerticalAlignment="Top"
                               Content="Remplissez les valeurs et Cliquez sur 'SUIVANT' pour démarrer l'envoi des statistiques" />

        <Controls:LabeledDatePickerControl x:Name="DateDeb"
                                           LabelText="Date de début de recherche : "
                                           Width="350"
                                           SelectedDateChanged="Date_SelectedDateChanged"
                                           LabelWidth="150" />
        <Controls:LabeledDatePickerControl x:Name="DateFin"
                                           LabelText="Date de fin de recherche : "
                                           Width="350"
                                           SelectedDateChanged="Date_SelectedDateChanged"
                                           LabelWidth="150" />

        <Controls:LabeledComboBoxControl x:Name="typeRech"
                                         LabelText="Type de recherche :"
                                         HorizontalAlignment="Left"
                                         Width="350"
                                         SelectionChanged="typeRech_SelectionChanged"
                                         LabelWidth="150" />

        <toolkit:DatePicker x:Name="Test"></toolkit:DatePicker>
        
        <Controls:TextBlockControl x:Name="InfosLabel"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Top" Text=""
                                   TextWrapping="Wrap" Width="800" Height="250"
                                   Margin="10,20,0,0" />
        
        <Controls:ProgressBarControl x:Name="progressBar"
                                     HorizontalAlignment="Center"
                                     VerticalAlignment="Top"
                                     ProgressBarWidth="180"
                                     Margin="0,20,0,0"/>
        
    </StackPanel>
    
</UserControl>


In the control "Test", the selectedDate is ok, but in the other controls (DateDeb, DateFin), the selectedDate is always null

Is that someone can help me ?

Thanks in advance.