From 46c25a2edbf257153d7bb33d2ac3997a3718e3ee Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Thu, 1 Dec 2016 14:15:17 -0700 Subject: Don't run Command CanExecute on incorrect inherited binding context type (#572) * Allow Command CanExecute to recover when run on inherited bindingcontext * Make exception handler more generic * Checking types in Command delegates to avoid exception in the first place * Adding type chekc to other Command constructor * Use nameof for ArgumentNullExceptions * Add unit tests for null parameters, handle value types and Nullable --- .../Bugzilla47971.cs | 109 +++++++++++++++++++++ .../Xamarin.Forms.Controls.Issues.Shared.projitems | 1 + 2 files changed, 110 insertions(+) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla47971.cs (limited to 'Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared') diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla47971.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla47971.cs new file mode 100644 index 00000000..351028d4 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla47971.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Runtime.CompilerServices; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Bugzilla, 47971, "UWP doesn't display list items when binding a CommandParameter to BindingContext in a DataTemplate and including a CanExecute method", PlatformAffected.WinRT)] + public class Bugzilla47971 : TestContentPage + { + protected override void Init() + { + var viewModel = new _47971ViewModel(); + + var lv = new ListView { BindingContext = viewModel }; + + lv.SetBinding(ListView.ItemsSourceProperty, new Binding("Models")); + lv.SetBinding(ListView.SelectedItemProperty, new Binding("SelectedModel")); + + lv.ItemTemplate = new DataTemplate(() => + { + var tc = new TextCell(); + + tc.SetBinding(TextCell.TextProperty, new Binding("Name")); + tc.SetBinding(TextCell.CommandParameterProperty, new Binding(".")); + tc.SetBinding(TextCell.CommandProperty, new Binding("BindingContext.ModelSelectedCommand", source: lv)); + + return tc; + }); + + var layout = new StackLayout { Spacing = 10 }; + var instructions = new Label {Text = "The ListView below should display three items (Item1, Item2, and Item3). If it does not, this test has failed." }; + + layout.Children.Add(instructions); + layout.Children.Add(lv); + + Content = layout; + } + + [Preserve(AllMembers = true)] + internal class _47971ViewModel : INotifyPropertyChanged + { + _47971ItemModel _selectedModel; + Command<_47971ItemModel> _modelSelectedCommand; + ObservableCollection<_47971ItemModel> _models; + + public ObservableCollection<_47971ItemModel> Models + { + get { return _models; } + set + { + _models = value; + OnPropertyChanged(); + } + } + + public _47971ItemModel SelectedModel + { + get { return _selectedModel; } + set + { + _selectedModel = value; + OnPropertyChanged(); + } + } + + public Command<_47971ItemModel> ModelSelectedCommand => _modelSelectedCommand ?? + (_modelSelectedCommand = new Command<_47971ItemModel>(ModelSelectedCommandExecute, CanExecute)); + + bool CanExecute(_47971ItemModel itemModel) + { + return true; + } + + void ModelSelectedCommandExecute(_47971ItemModel model) + { + System.Diagnostics.Debug.WriteLine(model.Name); + } + + public _47971ViewModel() + { + _models = new ObservableCollection<_47971ItemModel>( + new List<_47971ItemModel>() + { + new _47971ItemModel() { Name = "Item 1"}, + new _47971ItemModel() { Name = "Item 2"}, + new _47971ItemModel() { Name = "Item 3"} + }); + } + + public event PropertyChangedEventHandler PropertyChanged; + + protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + } + + [Preserve(AllMembers = true)] + internal class _47971ItemModel + { + public string Name { get; set; } + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index 3b728f4d..08c26ba1 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -139,6 +139,7 @@ + -- cgit v1.2.3