Bonjour à tous,

J'ai un souci avec mes Passwordbox en WPF en essayant de respecter le MVVM, j'arrive a le faire fonctionner sans souci pour ma vue de login, mais l'utilisateur à la possibilité de changer son mot de passe, donc il y a un Passwordbox , et un de confirmation.

Avec ce que j'utilise actuellement j'ai la possibilité de récupérer qu'un seul Passwordbox.

Code de ma vue:
Code XAML : 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<Window x:Class="V_RemoteAccess"
        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"
        xmlns:local="clr-namespace:MonLogiciel"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        ResizeMode="NoResize" 
        SizeToContent ="WidthAndHeight"
        Title="{Binding FORM_MyTitle_Text,FallbackValue='Access level'}" 
        Height="{Binding MyHeight, FallbackValue='Auto'}" Width="300"
         Background="{Binding FORM_General_Background,FallbackValue='Bisque'}"
        Loaded="Window_Loaded"
         Icon="/WinCoSSMIC;component/WinCoSSMIC.ico"
        KeyDown="V_RemoteAccess_KeyDown">
    <Grid Margin="0,0,2,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <GroupBox Grid.Row="0"
                  Margin="5"
                  Header="{Binding GB_AccessType_Text,FallbackValue='Access Type'}"
                  Foreground="{Binding LBL_General_Foreground,FallbackValue='Black'}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*"/>
                    <ColumnDefinition Width="1*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <RadioButton x:Name="RB_User"
                             Grid.Column="0"
                             Grid.Row="0"
                             Margin="10,5,0,5"
                             HorizontalAlignment="left"  
                             VerticalAlignment="Center"
                             Content="{Binding RB_User_Text,FallbackValue='User'}"
                             IsChecked="{Binding RB_User_Selected,FallbackValue='True'}"
                             Command="{Binding Path=MyRadioCommand}" 
                             CommandParameter="{Binding ElementName=RB_User, Path=Name}"
                             Foreground="{Binding LBL_General_Foreground,FallbackValue='Black'}"/>
                <RadioButton x:Name="RB_SuperUser"
                             Grid.Column="0"
                             Grid.Row="1"
                             Margin="10,5,0,5"
                             HorizontalAlignment="Left"  
                             VerticalAlignment="Center"
                             Content="{Binding RB_SuperUser_Text,FallbackValue='Super User'}"
                             IsChecked="{Binding RB_SuperUser_Selected,FallbackValue='false'}"
                              Command="{Binding Path=MyRadioCommand}" 
                             CommandParameter="{Binding ElementName=RB_SuperUser, Path=Name}"
                             Foreground="{Binding LBL_General_Foreground,FallbackValue='Black'}"/>
                <RadioButton x:Name="RB_Admin"
                             Grid.Column="1"
                             Grid.Row="0"
                             Margin="10,5,0,5"
                             HorizontalAlignment="Left" 
                             VerticalAlignment="Center"
                             Content="{Binding RB_Admin_Text,FallbackValue='Administrator'}"
                             IsChecked="{Binding RB_Admin_Selected,FallbackValue='false'}"
                             Command="{Binding Path=MyRadioCommand}" 
                             CommandParameter="{Binding ElementName=RB_Admin, Path=Name}"
                             Foreground="{Binding LBL_General_Foreground,FallbackValue='Black'}"/>
                <RadioButton x:Name="RB_Factory"
                             Grid.Column="1"
                             Grid.Row="1"
                             Margin="10,5,0,5"
                             HorizontalAlignment="Left" 
                             VerticalAlignment="Center"
                             Content="{Binding RB_Factory_Text,FallbackValue='factory'}"
                             IsChecked="{Binding RB_Factory_Selected,FallbackValue='false'}"
                             Command="{Binding Path=MyRadioCommand}" 
                             CommandParameter="{Binding ElementName=RB_Factory, Path=Name}"
                             Foreground="{Binding LBL_General_Foreground,FallbackValue='Black'}"/>
            </Grid>
        </GroupBox>
        <GroupBox Grid.Row="1"
                  Margin="5"
                  Header="{Binding GB_Password_Text,FallbackValue='Password'}"
                  Foreground="{Binding LBL_General_Foreground,FallbackValue='Black'}">
 
            <PasswordBox x:Name="TXT_Password"
                         Margin="5"
                         Height="25"
                         Style="{DynamicResource  PasswordBoxRound }"
                         Foreground="{Binding TXT_General_Foreground,FallbackValue='Yellow'}"
                         Background="{Binding TXT_General_Background,FallbackValue='Gray'}"/>
        </GroupBox>
        <GroupBox Grid.Row="2"
                  Margin="5"
                  Header="{Binding GB_ConfirmPwd_Text,FallbackValue='Confirm password'}"
                  Visibility="{Binding GB_ConfirmPwd_Visibility}"
                  Foreground="{Binding LBL_General_Foreground,FallbackValue='Black'}">
 
            <PasswordBox x:Name="TXT_Confirm"
                         Margin="5"
                         Height="25"
                         Style="{DynamicResource  PasswordBoxRound }"
                         Foreground="{Binding TXT_General_Foreground,FallbackValue='Yellow'}"
                         Background="{Binding TXT_General_Background,FallbackValue='Gray'}"/>
        </GroupBox>
        <Grid x:Name="GridButton"
             Grid.Row="3">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="1*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="cmd_Access"
                Content="{Binding CMD_Access_Text,FallbackValue='Access'}"
                    Style="{StaticResource MyButton}"
                    Grid.Column ="0"
                    Margin="5,5,5,10" 
                    Width="100" Height="25"
                    Visibility="{Binding CMD_access_Visibility}"
                    Command="{Binding LoginCommand}"
                    CommandParameter="{Binding ElementName=V_RemoteAccess}"
                  />
            <Button Content="{Binding CMD_Abort_Text,FallbackValue='Abort'}"
                    Style="{StaticResource MyButton}"
                    IsEnabled="{Binding CMD_Abort_Enabled, FallbackValue='True'}"
                    Grid.Column ="1"
                    Margin="5,5,5,10" 
                    Width="100" Height=" 25"
                    Visibility="{Binding CMD_Abort_Visibility}"
                    Command="{Binding SelectCommand}"
                    CommandParameter="ABORT"/>
        </Grid>
    </Grid>
</Window>


Code behind:
Code : 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
Imports System.Security
Public Class V_RemoteAccess
    Implements IHavePassword
    Public Sub New()
 
        ' Cet appel est requis par le concepteur.
        InitializeComponent()
 
        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        RemoteAccess_VM = New VM_RemoteAccess(Me)
        AddHandler RemoteAccess_VM.SetPassWord, AddressOf SetPassword
        AddHandler RemoteAccess_VM.SetConfirmPassWord, AddressOf SetConfirmPassword
        DataContext = RemoteAccess_VM
 
 
    End Sub
 
    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
        TXT_Password.Focus()
        TXT_Password.SelectAll()
 
        Dim Focus As IRequestFocus = DirectCast(DataContext, IRequestFocus)
        AddHandler Focus.FocusRequested, AddressOf OnFocusRequested
    End Sub
    Private Sub OnFocusRequested(sender As Object, e As FocusRequestedEventArgs)
        Dim ViewModel = CType(DataContext, VM_RemoteAccess)
        Select Case e.PropertyName
 
            Case NameOf(ViewModel.TXT_ConfirmPwd_Text)
                TXT_Confirm.Focus()
                TXT_Confirm.SelectAll()
 
        End Select
    End Sub
 
    Private ReadOnly Property IHavePassword_Password As SecureString Implements IHavePassword.Password
        Get
             'Ici je n'est qu'un seul mot de passe possible.
            Return TXT_Password.SecurePassword
        End Get
    End Property
 
    Private Sub SetPassword(ByVal pass As String)
        TXT_Password.Password = pass
    End Sub
    Private Sub SetConfirmPassword(pass As String)
        TXT_Confirm.Password = pass
    End Sub
 
    Private Sub V_RemoteAccess_KeyDown(sender As Object, e As KeyEventArgs)
        If (e.Key = Key.Enter) Then
            If TXT_Password.Password.Count > 0 Then
                RemoteAccess_VM.UserPassword_KeyDown_Enter(Me)
            End If
        End If
    End Sub
End Class
Code VM:
Code : 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
 
    Private _PasswordInVM As String
    Public Property PasswordInVM() As String
        Get
            Return _PasswordInVM
        End Get
        Set(ByVal value As String)
            _PasswordInVM = value
        End Set
    End Property
    Private _PasswordConfiInVM As String
    Public Property PasswordConfiInVM() As String
        Get
            Return _PasswordConfiInVM
        End Get
        Set(ByVal value As String)
            _PasswordConfiInVM = value
        End Set
    End Property
  Private Sub Login(ByVal parameter As Object)
        Try
               'MyWindo c'est ma vue XAML ci dessus, c'est pas trop MVVM sa, mais c'est ce que j'ai trouvé pour avoir des passwordbox.
            Dim passwordContainer = TryCast(MyWindo, IHavePassword)
            If passwordContainer IsNot Nothing Then
                Dim secureString = passwordContainer.Password
                PasswordInVM = ConvertToUnsecureString(secureString)
                TXT_Password_text = PasswordInVM
                CMD_access_Click()
 
            End If
        Catch ex As Exception
 
        End Try
 
    End Sub
Pour le moment je vais faire une vue intermédiaire, dans la vue ci dessus il n'y aura qu'un seul mot de passe, puis après validation, une seconde vue avec juste la confirmation du mot de passe.

Merci de votre aide