Skip to content

Commit 080cf81

Browse files
committed
fix(passwordbox): Don't fail on setting null on PasswordBox.Password
1 parent eb30a11 commit 080cf81

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Linq;
5+
using System.Runtime.CompilerServices;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
using Windows.UI.Xaml;
10+
using Windows.UI.Xaml.Controls;
11+
using Windows.UI.Xaml.Data;
12+
13+
namespace Uno.UI.Tests.PasswordBoxTests
14+
{
15+
[TestClass]
16+
public class Given_PasswordBox
17+
{
18+
[TestMethod]
19+
public void When_Binding_Set_Null()
20+
{
21+
var passwordBox = new PasswordBox();
22+
var source = new MySource() { SourceText = "Spinach" };
23+
24+
passwordBox.DataContext = source;
25+
passwordBox.SetBinding(PasswordBox.PasswordProperty, new Binding() { Path = new PropertyPath("SourceText") });
26+
27+
Assert.AreEqual("Spinach", passwordBox.Password);
28+
29+
source.SourceText = null;
30+
31+
Assert.AreEqual("", passwordBox.Password);
32+
}
33+
34+
[TestMethod]
35+
public void When_Set_DP_Null()
36+
{
37+
var passwordBox = new PasswordBox();
38+
39+
passwordBox.SetValue(PasswordBox.PasswordProperty, "Spinach");
40+
Assert.AreEqual("Spinach", passwordBox.Password);
41+
passwordBox.SetValue(PasswordBox.PasswordProperty, null);
42+
Assert.AreEqual("", passwordBox.Password);
43+
}
44+
45+
[TestMethod]
46+
public void When_Binding_Set_Non_String()
47+
{
48+
var passwordBox = new PasswordBox();
49+
var source = new MySource() { SourceInt = 12 };
50+
51+
passwordBox.DataContext = source;
52+
passwordBox.SetBinding(PasswordBox.PasswordProperty, new Binding() { Path = new PropertyPath("SourceInt") });
53+
54+
Assert.AreEqual("12", passwordBox.Password);
55+
56+
source.SourceInt = 19;
57+
58+
Assert.AreEqual("19", passwordBox.Password);
59+
}
60+
61+
public class MySource : System.ComponentModel.INotifyPropertyChanged
62+
{
63+
private string _sourceText;
64+
65+
public string SourceText
66+
{
67+
get => _sourceText;
68+
set
69+
{
70+
if (_sourceText != value)
71+
{
72+
_sourceText = value;
73+
OnPropertyChanged();
74+
}
75+
}
76+
}
77+
78+
private int _sourceInt;
79+
80+
public int SourceInt
81+
{
82+
get => _sourceInt;
83+
set
84+
{
85+
if (_sourceInt != value)
86+
{
87+
_sourceInt = value;
88+
OnPropertyChanged();
89+
}
90+
}
91+
}
92+
93+
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
94+
95+
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
96+
=> PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
97+
}
98+
}
99+
}

src/Uno.UI/UI/Xaml/Controls/PasswordBox/PasswordBox.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public string Password
103103

104104
private void OnPasswordChanged(DependencyPropertyChangedEventArgs e)
105105
{
106-
Text = (string)e.NewValue;
106+
SetValue(TextProperty, (string)e.NewValue);
107107

108108
PasswordChanged?.Invoke(this, new RoutedEventArgs(this));
109109

@@ -124,7 +124,7 @@ protected override void OnTextChanged(DependencyPropertyChangedEventArgs e)
124124
{
125125
base.OnTextChanged(e);
126126

127-
Password = (string)e.NewValue;
127+
SetValue(PasswordProperty, (string)e.NewValue);
128128
}
129129

130130
protected override AutomationPeer OnCreateAutomationPeer()

0 commit comments

Comments
 (0)