Bonjour,

J'ai une BDD avec un champ DateTime.
Je veux donc enregistrer dans ma BDD la date et l'heure
Le probleme et qu'il y a que la date qui s'enregistre.

Ma premiere solution est d'utiliser un DatePicker et un TimePicker dans ma DataForm:
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
 
<toolkit:DataField>
   <sdk:DatePicker SelectedDate="{Binding Path=Debut,Mode=TwoWay}"   />
</toolkit:DataField>
<toolkit:DataField>
    <toolkit:TimePicker Value="{Binding Path=Debut,Mode=TwoWay}" />
</toolkit:DataField>

La date et l'heure change bien dans le context mais il y a que la date qui s'enregistre dans la BDD.

ensuite j'ai fais un UserControl et j'ai le même probleme
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
<UserControl x:Class="VEH.DateTimePicker"
    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/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    d:DesignHeight="30" d:DesignWidth="250">
 
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.65*"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
 
        <sdk:DatePicker Height="23" Name="dpMain" VerticalAlignment="Center" Margin="0,4" />
        <toolkit:TimePicker Grid.Column="1" Height="23" HorizontalAlignment="Stretch"  Name="tpMain" VerticalAlignment="Center" />
    </Grid>
</UserControl>

Code vb.net : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
 
'****************************
' * :<my3:DateTimePicker  DisplayData="{Binding Mode=TwoWay, Path=StartDate, NotifyOnValidationError=True}" Width="230" />
' * *****************************
 
Partial Public Class DateTimePicker
    Inherits UserControl
 
    Public Sub New()
        InitializeComponent()
    End Sub
    Public Shared ReadOnly DisplayDataProperty As DependencyProperty = DependencyProperty.Register("DisplayData", GetType(DateTime), GetType(DateTimePicker), New PropertyMetadata(New PropertyChangedCallback(AddressOf DisplayDataPropertyCallBack)))
    ''' <summary>
    ''' 
    ''' </summary>
    Public Property DisplayData() As DateTime
        Get
            Return CType(Me.GetValue(DisplayDataProperty), DateTime)
        End Get
        Set(value As DateTime)
            SetValue(DisplayDataProperty, value)
        End Set
    End Property
 
    Public Shared Sub DisplayDataPropertyCallBack(sender As Object, args As DependencyPropertyChangedEventArgs)
 
        Dim sel As DateTimePicker = TryCast(sender, DateTimePicker)
        If sel IsNot Nothing Then
            If args.NewValue IsNot Nothing Then
                Dim dt As DateTime
                If DateTime.TryParse(args.NewValue.ToString(), dt) Then
                    sel.SetDataTime(dt)
                End If
            End If
        End If
    End Sub
 
    Private isLoaded As Boolean = False
    Private Sub SetDataTime(datatime As DateTime)
        isLoaded = False
        Try
            dpMain.Text = datatime.ToString()
            tpMain.Value = datatime
 
            RemoveHandler dpMain.SelectedDateChanged, AddressOf dpMain_SelectedDateChanged
            AddHandler dpMain.SelectedDateChanged, AddressOf dpMain_SelectedDateChanged
 
            RemoveHandler tpMain.ValueChanged, AddressOf tpMain_ValueChanged
            AddHandler tpMain.ValueChanged, AddressOf tpMain_ValueChanged
        Catch ex As Exception
            ErrorWindow.CreateNew(ex)
        End Try
        isLoaded = True
    End Sub
 
    Private Sub dpMain_SelectedDateChanged(sender As Object, e As SelectionChangedEventArgs)
        If isLoaded = False Then
            Return
        End If
 
        Dim hour As Integer = DisplayData.Hour
        Dim minute As Integer = DisplayData.Minute
        Dim second As Integer = DisplayData.Second
        If dpMain.SelectedDate IsNot Nothing Then
 
            If dpMain.SelectedDate.Value.Hour = 0 AndAlso dpMain.SelectedDate.Value.Minute = 0 Then
                Dim datetime As DateTime = dpMain.SelectedDate.Value.AddHours(hour).AddMinutes(minute).AddSeconds(second)
 
                DisplayData = datetime
            Else
                DisplayData = dpMain.SelectedDate.Value
            End If
        End If
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub tpMain_ValueChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of System.Nullable(Of DateTime)))
        If isLoaded = False Then
            Return
        End If
 
        Dim year As Integer = DisplayData.Year
        Dim month As Integer = DisplayData.Month
        Dim day As Integer = DisplayData.Day
 
        If tpMain.Value IsNot Nothing Then
            Dim dt As String = year.ToString() & "/" & month.ToString() & "/" & day.ToString() & " " & tpMain.Value.Value.Hour.ToString() & ":" & tpMain.Value.Value.Minute.ToString() & ":" & tpMain.Value.Value.Second.ToString()
            Dim d As DateTime = Convert.ToDateTime(dt)
            DisplayData = d
        End If
    End Sub
 
End Class

Avez vous des idées pour que je puisse enregistrer la date et l'heure svp?
Merci