Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextBox - reset Text when pushing null to bound value #1303

Merged
merged 1 commit into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/ReleaseNotes/_ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* `TextBox` no longer raises TextChanged when its template is applied, in line with UWP.
* `TextBox.TextChanged` is now called asynchronously after the UI is updated, in line with UWP. For most uses `TextChanging` should be preferred.
* [Android] `TextBox.IsSpellCheckEnabled = false` is now enforced in a way that may cause issues in certain use cases (see https://stackoverflow.com/a/5188119/1902058). The old behavior can be restored by setting `ShouldForceDisableSpellCheck = false`, per `TextBox`.
* `TextBox.Text = null` will now throw an exception, as on UWP. Pushing `null` via a binding is still valid.

### Bug fixes
* #1276 retrieving non-existent setting via indexer should not throw and `ApplicationDataContainer` allowed clearing value by calling `Add(null)` which was not consistent with UWP.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace Uno.UI.Tests.TextBoxTests
{
Expand Down Expand Up @@ -40,17 +44,45 @@ public void When_Setting_Text_Null()
textBox.Text = "Rhubarb";
Assert.AreEqual("Rhubarb", textBox.Text);
Assert.AreEqual(1, callbackCount);

#if NETFX_CORE

Assert.ThrowsException<ArgumentNullException>(() => textBox.Text = null);
#else
textBox.Text = null;
#endif
;

Assert.AreEqual("Rhubarb", textBox.Text);
Assert.AreEqual(1, callbackCount);
}

[TestMethod]
public void When_Binding_Set_Null()
{
var textBox = new TextBox();
var source = new MySource() { SourceText = "Spinach" };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love your samples ;-)


textBox.DataContext = source;
textBox.SetBinding(TextBox.TextProperty, new Binding() { Path = new PropertyPath("SourceText") });

Assert.AreEqual("Spinach", textBox.Text);

source.SourceText = null;

Assert.AreEqual("", textBox.Text);
}

[TestMethod]
public void When_Binding_Set_Non_String()
{
var textBox = new TextBox();
var source = new MySource() { SourceInt = 12 };

textBox.DataContext = source;
textBox.SetBinding(TextBox.TextProperty, new Binding() { Path = new PropertyPath("SourceInt") });

Assert.AreEqual("12", textBox.Text);

source.SourceInt = 19;

Assert.AreEqual("19", textBox.Text);
}

[TestMethod]
public void When_BeforeTextChanging_Cancel()
{
Expand Down Expand Up @@ -80,5 +112,43 @@ public void When_BeforeTextChanging_Cancel()
textBox.Text = "Chirimoya";
Assert.AreEqual(2, beforeTextChangingCount);
}

public class MySource : INotifyPropertyChanged
{
private string _sourceText;

public string SourceText
{
get => _sourceText;
set
{
if (_sourceText != value)
{
_sourceText = value;
OnPropertyChanged();
}
}
}

private int _sourceInt;

public int SourceInt
{
get => _sourceInt;
set
{
if (_sourceInt != value)
{
_sourceInt = value;
OnPropertyChanged();
}
}
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
11 changes: 9 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,14 @@ private DependencyPropertyChangedEventArgs CreateInitialValueChangerEventArgs(De
public string Text
{
get { return (string)this.GetValue(TextProperty); }
set { this.SetValue(TextProperty, value); }
set {
if (value == null)
{
throw new ArgumentNullException();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it the behavior of UWP? Surprising...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep

}

this.SetValue(TextProperty, value);
}
}

public static readonly DependencyProperty TextProperty =
Expand Down Expand Up @@ -243,7 +250,7 @@ private object CoerceText(object baseValue)
{
if (!(baseValue is string baseString))
{
return DependencyProperty.UnsetValue; //TODO: UWP throws ArgumentNullException, in principle we should do the same.
return ""; //Pushing null to the binding resets the text. (Setting null to the Text property directly throws an exception.)
}

var args = new TextBoxBeforeTextChangingEventArgs(baseString);
Expand Down