yavsc/BookAStar/BookAStar/Views/UserListView.xaml.cs

97 lines
3.2 KiB
C#
Raw Normal View History

2016-10-26 20:29:46 +02:00
using BookAStar.Model;
2016-12-16 01:13:16 +01:00
using BookAStar.Model.Social.Chat;
2016-12-15 14:19:53 +01:00
using BookAStar.ViewModels.Messaging;
2016-10-26 20:29:46 +02:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2016-12-15 14:19:53 +01:00
using System.Windows.Input;
2016-10-26 20:29:46 +02:00
using Xamarin.Forms;
namespace BookAStar.Views
{
public partial class UserListView : ContentView
{
public BindableProperty ItemsSourceProperty = BindableProperty.Create(
2016-12-15 14:19:53 +01:00
"ItemsSource", typeof(ChatUserCollection), typeof(UserListView), default(ChatUserCollection),
BindingMode.OneWay);
2016-12-16 01:13:16 +01:00
public BindableProperty ItemSelectedCommandProperty = BindableProperty.Create(
"ItemSelectedCommand", typeof(ICommand), typeof(UserListView), default(ICommand));
2016-10-26 20:29:46 +02:00
2016-12-15 14:19:53 +01:00
public BindableProperty DisableSelectionProperty = BindableProperty.Create(
"DisableSelection", typeof(bool), typeof(UserListView), false);
2016-12-16 01:13:16 +01:00
public BindableProperty HasASelectionProperty = BindableProperty.Create(
"HasASelection", typeof(bool), typeof(UserListView), false);
public BindableProperty SelectedUserProperty = BindableProperty.Create(
"SelectedUser", typeof(ChatUserInfo), typeof(UserListView), default(ChatUserInfo));
2016-12-15 14:19:53 +01:00
public ChatUserCollection ItemsSource
2016-10-26 20:29:46 +02:00
{
2016-12-15 14:19:53 +01:00
get { return (ChatUserCollection) GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
2016-12-16 01:13:16 +01:00
public ChatUserInfo SelectedUser
{
get
{
return (ChatUserInfo) GetValue(SelectedUserProperty);
}
}
public ICommand ItemSelectedCommand
2016-12-15 14:19:53 +01:00
{
2016-12-16 01:13:16 +01:00
get { return (ICommand) GetValue(ItemSelectedCommandProperty); }
set { SetValue(ItemSelectedCommandProperty, value); }
2016-10-26 20:29:46 +02:00
}
2016-12-15 14:19:53 +01:00
public bool DisableSelection
2016-10-26 20:29:46 +02:00
{
2016-12-15 14:19:53 +01:00
get { return (bool) GetValue(DisableSelectionProperty); }
set { SetValue(DisableSelectionProperty, value); }
2016-10-26 20:29:46 +02:00
}
2016-12-15 14:19:53 +01:00
2016-12-16 01:13:16 +01:00
public bool HasASelection
{
get { return (bool)GetValue(HasASelectionProperty); }
set { SetValue(HasASelectionProperty, value); }
}
2016-10-26 20:29:46 +02:00
public UserListView()
{
InitializeComponent();
2016-12-15 14:19:53 +01:00
list.ItemSelected += OnUserSelected;
}
2016-12-16 01:13:16 +01:00
2016-12-15 14:19:53 +01:00
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
ICommand dataCommand = (ICommand) BindingContext;
list.RefreshCommand = new Command(
() => {
dataCommand.Execute(null);
list.EndRefresh();
});
}
2016-12-16 01:13:16 +01:00
2016-12-15 14:19:53 +01:00
public void OnUserSelected(object sender, SelectedItemChangedEventArgs ev)
{
2016-12-16 01:13:16 +01:00
if (ItemSelectedCommand != null)
if (ItemSelectedCommand.CanExecute(ev.SelectedItem))
2016-12-15 14:19:53 +01:00
{
2016-12-16 01:13:16 +01:00
ItemSelectedCommand.Execute(ev.SelectedItem);
2016-12-15 14:19:53 +01:00
}
2016-12-16 01:13:16 +01:00
if (DisableSelection)
list.SelectedItem = null;
SetValue(SelectedUserProperty, list.SelectedItem);
HasASelection = list.SelectedItem != null;
2016-12-15 14:19:53 +01:00
}
2016-12-16 01:13:16 +01:00
2016-10-26 20:29:46 +02:00
}
}