-
Notifications
You must be signed in to change notification settings - Fork 4
Added SelectedItemChanged event #1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,55 +1,134 @@ | ||
| using System.Collections; | ||
| using System; | ||
| using System.Collections; | ||
| using System.Windows.Input; | ||
| using Xamarin.Forms; | ||
|
|
||
| namespace XFBindableStackLayout | ||
| { | ||
| public class BindableStackLayout : StackLayout | ||
| { | ||
| readonly Label header; | ||
|
|
||
| public BindableStackLayout() | ||
| { | ||
| header = new Label(); | ||
| Children.Add(header); | ||
|
|
||
| ItemSelectedCommand = new Command<object>(item => | ||
| { | ||
| SelectedItem = item; | ||
| }); | ||
| } | ||
|
|
||
| public event EventHandler<SelectedItemChangedEventArgs> SelectedItemChanged; | ||
|
|
||
| public IEnumerable ItemsSource | ||
| { | ||
| get { return (IEnumerable)GetValue(ItemsSourceProperty); } | ||
| set { SetValue(ItemsSourceProperty, value); } | ||
| } | ||
| public static readonly BindableProperty ItemsSourceProperty = | ||
| BindableProperty.Create(nameof(ItemsSource), typeof(IEnumerable), typeof(BindableStackLayout), | ||
| propertyChanged: (bindable, oldValue, newValue) => ((BindableStackLayout)bindable).PopulateItems()); | ||
| public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create( | ||
| nameof(ItemsSource), | ||
| typeof(IEnumerable), | ||
| typeof(BindableStackLayout), | ||
| propertyChanged: (bindable, oldValue, newValue) => ((BindableStackLayout)bindable).PopulateItems() | ||
| ); | ||
|
|
||
| public DataTemplate ItemDataTemplate | ||
| { | ||
| get { return (DataTemplate)GetValue(ItemDataTemplateProperty); } | ||
| set { SetValue(ItemDataTemplateProperty, value); } | ||
| } | ||
| public static readonly BindableProperty ItemDataTemplateProperty = | ||
| BindableProperty.Create(nameof(ItemDataTemplate), typeof(DataTemplate), typeof(BindableStackLayout)); | ||
| public static readonly BindableProperty ItemDataTemplateProperty = BindableProperty.Create( | ||
| nameof(ItemDataTemplate), typeof(DataTemplate), typeof(BindableStackLayout) | ||
| ); | ||
|
|
||
| public string Title | ||
| { | ||
| get { return (string)GetValue(TitleProperty); } | ||
| set { SetValue(TitleProperty, value); } | ||
| } | ||
| public static readonly BindableProperty TitleProperty = | ||
| BindableProperty.Create(nameof(Title), typeof(string), typeof(BindableStackLayout), | ||
| propertyChanged: (bindable, oldValue, newValue) => ((BindableStackLayout)bindable).PopulateHeader()); | ||
| public static readonly BindableProperty TitleProperty = BindableProperty.Create( | ||
| nameof(Title), typeof(string), | ||
| typeof(BindableStackLayout), | ||
| propertyChanged: (bindable, oldValue, newValue) => ((BindableStackLayout)bindable).PopulateHeader() | ||
| ); | ||
|
|
||
| public object SelectedItem | ||
| { | ||
| get { return GetValue(SelectedItemProperty); } | ||
| set { SetValue(SelectedItemProperty, value); } | ||
| } | ||
|
|
||
| public static BindableProperty SelectedItemProperty = BindableProperty.Create( | ||
| propertyName: "SelectedItem", | ||
| returnType: typeof(object), | ||
| declaringType: typeof(BindableStackLayout), | ||
| defaultValue: null, | ||
| defaultBindingMode: BindingMode.OneWay, | ||
| propertyChanged: OnSelectedItemChanged | ||
| ); | ||
|
|
||
| private static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In order to keep the code style consistent the access modifier |
||
| { | ||
| var itemsView = (BindableStackLayout)bindable; | ||
| if (newValue == oldValue) | ||
| return; | ||
|
|
||
| itemsView.SetSelectedItem(newValue); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method |
||
| } | ||
|
|
||
| protected virtual void SetSelectedItem(object selectedItem) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why P.S.: In order to keep the code style consistent the access modifier |
||
| { | ||
| var handler = SelectedItemChanged; | ||
| if (handler != null) | ||
| handler(this, new SelectedItemChangedEventArgs(selectedItem)); | ||
| } | ||
|
|
||
| void PopulateItems() | ||
| { | ||
| if (ItemsSource == null) return; | ||
| foreach (var item in ItemsSource) | ||
| { | ||
| var itemTemplate = ItemDataTemplate.CreateContent() as View; | ||
| itemTemplate.BindingContext = item; | ||
| Children.Add(itemTemplate); | ||
| Children.Add(GetItemView(item)); | ||
| } | ||
| } | ||
|
|
||
| void PopulateHeader() => header.Text = Title; | ||
|
|
||
| protected virtual View GetItemView(object item) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why P.S.: In order to keep the code style consistent the access modifier |
||
| { | ||
| var content = ItemDataTemplate.CreateContent(); | ||
|
|
||
| var view = content as View; | ||
| if (view == null) | ||
| return null; | ||
|
|
||
| view.BindingContext = item; | ||
|
|
||
| var gesture = new TapGestureRecognizer | ||
| { | ||
| Command = ItemSelectedCommand, | ||
| CommandParameter = item | ||
| }; | ||
|
|
||
| AddGesture(view, gesture); | ||
|
|
||
| return view; | ||
| } | ||
|
|
||
| protected readonly ICommand ItemSelectedCommand; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why P.S.: In order to keep the code style consistent the access modifier |
||
|
|
||
| protected void AddGesture(View view, TapGestureRecognizer gesture) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
P.S.: In order to keep the code style consistent the access modifier |
||
| { | ||
| view.GestureRecognizers.Add(gesture); | ||
|
|
||
| var layout = view as Layout<View>; | ||
|
|
||
| if (layout == null) | ||
| return; | ||
|
|
||
| foreach (var child in layout.Children) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need to add a |
||
| AddGesture(child, gesture); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,11 +7,12 @@ | |
| <ContentPage.BindingContext> | ||
| <local:MainViewModel /> | ||
| </ContentPage.BindingContext> | ||
| <local:BindableStackLayout | ||
| <local:BindableStackLayout | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If possible, it will make sense to add a sample of binding to |
||
| Title="Colors:" | ||
| ItemsSource="{Binding MyColors}" | ||
| VerticalOptions="Center" | ||
| HorizontalOptions="Center"> | ||
| HorizontalOptions="Center" | ||
| SelectedItemChanged="OnSelectedItemChanged"> | ||
| <local:BindableStackLayout.ItemDataTemplate> | ||
| <DataTemplate> | ||
| <StackLayout | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BindingMode should be set to BindingMode.TwoWay.