Updated controls (added SelectedItem property).
This commit is contained in:
Родитель
d9814a336b
Коммит
43961a9fd6
Двоичные данные
HorizontalListView/.vs/HorizontalListView/v14/.suo
Двоичные данные
HorizontalListView/.vs/HorizontalListView/v14/.suo
Двоичный файл не отображается.
|
@ -262,6 +262,8 @@ Global
|
|||
{B18D6F4A-99DD-48BE-BEA2-2F63ACD0EE3F}.AppStore|x86.Build.0 = Release|x86
|
||||
{B18D6F4A-99DD-48BE-BEA2-2F63ACD0EE3F}.AppStore|x86.Deploy.0 = Release|x86
|
||||
{B18D6F4A-99DD-48BE-BEA2-2F63ACD0EE3F}.Debug|Any CPU.ActiveCfg = Debug|x86
|
||||
{B18D6F4A-99DD-48BE-BEA2-2F63ACD0EE3F}.Debug|Any CPU.Build.0 = Debug|x86
|
||||
{B18D6F4A-99DD-48BE-BEA2-2F63ACD0EE3F}.Debug|Any CPU.Deploy.0 = Debug|x86
|
||||
{B18D6F4A-99DD-48BE-BEA2-2F63ACD0EE3F}.Debug|ARM.ActiveCfg = Debug|ARM
|
||||
{B18D6F4A-99DD-48BE-BEA2-2F63ACD0EE3F}.Debug|ARM.Build.0 = Debug|ARM
|
||||
{B18D6F4A-99DD-48BE-BEA2-2F63ACD0EE3F}.Debug|ARM.Deploy.0 = Debug|ARM
|
||||
|
|
|
@ -6,8 +6,20 @@ namespace HorizontalListViewSample.ViewModels
|
|||
{
|
||||
public class HorizontalMonkeysViewModel : ViewModelBase
|
||||
{
|
||||
private Monkey _selectedMonkey;
|
||||
|
||||
public ObservableCollection<Monkey> Monkeys { get; set; }
|
||||
|
||||
public Monkey SelectedMonkey
|
||||
{
|
||||
get { return _selectedMonkey; }
|
||||
set
|
||||
{
|
||||
_selectedMonkey = value;
|
||||
RaisePropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public HorizontalMonkeysViewModel()
|
||||
{
|
||||
Monkeys = new ObservableCollection<Monkey>
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
<controls:HorizontalListView
|
||||
Grid.Row="0"
|
||||
ItemsSource="{Binding Monkeys}"
|
||||
SelectedItem="{Binding SelectedMonkey, Mode=TwoWay}"
|
||||
HeightRequest="200">
|
||||
<controls:HorizontalListView.ItemTemplate>
|
||||
<DataTemplate>
|
||||
|
@ -21,5 +22,10 @@
|
|||
</DataTemplate>
|
||||
</controls:HorizontalListView.ItemTemplate>
|
||||
</controls:HorizontalListView>
|
||||
<Label
|
||||
Grid.Row="1"
|
||||
Text="{Binding SelectedMonkey.Name}"
|
||||
TextColor="Black"
|
||||
FontSize="Medium"/>
|
||||
</Grid>
|
||||
</ContentPage>
|
|
@ -18,6 +18,16 @@ namespace HorizontalListView.Controls
|
|||
BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(HorizontalListView), default(IEnumerable<object>),
|
||||
BindingMode.TwoWay, propertyChanged: ItemsSourceChanged);
|
||||
|
||||
public object SelectedItem
|
||||
{
|
||||
get { return (object)GetValue(SelectedItemProperty); }
|
||||
set { SetValue(SelectedItemProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly BindableProperty SelectedItemProperty =
|
||||
BindableProperty.Create("SelectedItem", typeof(object), typeof(HorizontalListView), default(object),
|
||||
BindingMode.TwoWay, null);
|
||||
|
||||
public DataTemplate ItemTemplate
|
||||
{
|
||||
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
|
||||
|
@ -66,9 +76,17 @@ namespace HorizontalListView.Controls
|
|||
{
|
||||
var content = ItemTemplate.CreateContent();
|
||||
var view = content as View;
|
||||
|
||||
var tapEvent = new TapGestureRecognizer();
|
||||
tapEvent.Tapped += (c, r) =>
|
||||
{
|
||||
SelectedItem = item;
|
||||
};
|
||||
|
||||
if (view == null)
|
||||
return null;
|
||||
|
||||
view.GestureRecognizers.Add(tapEvent);
|
||||
view.BindingContext = item;
|
||||
|
||||
return view;
|
||||
|
|
Двоичные данные
WrapView/.vs/WrapView/v14/.suo
Двоичные данные
WrapView/.vs/WrapView/v14/.suo
Двоичный файл не отображается.
|
@ -262,6 +262,8 @@ Global
|
|||
{AFA1B1E4-B424-429A-960F-E07356118135}.AppStore|x86.Build.0 = Release|x86
|
||||
{AFA1B1E4-B424-429A-960F-E07356118135}.AppStore|x86.Deploy.0 = Release|x86
|
||||
{AFA1B1E4-B424-429A-960F-E07356118135}.Debug|Any CPU.ActiveCfg = Debug|x86
|
||||
{AFA1B1E4-B424-429A-960F-E07356118135}.Debug|Any CPU.Build.0 = Debug|x86
|
||||
{AFA1B1E4-B424-429A-960F-E07356118135}.Debug|Any CPU.Deploy.0 = Debug|x86
|
||||
{AFA1B1E4-B424-429A-960F-E07356118135}.Debug|ARM.ActiveCfg = Debug|ARM
|
||||
{AFA1B1E4-B424-429A-960F-E07356118135}.Debug|ARM.Build.0 = Debug|ARM
|
||||
{AFA1B1E4-B424-429A-960F-E07356118135}.Debug|ARM.Deploy.0 = Debug|ARM
|
||||
|
|
|
@ -6,8 +6,20 @@ namespace WrapViewSample.ViewModels
|
|||
{
|
||||
public class MonkeysViewModel : ViewModelBase
|
||||
{
|
||||
private Monkey _selectedMonkey;
|
||||
|
||||
public ObservableCollection<Monkey> Monkeys { get; set; }
|
||||
|
||||
public Monkey SelectedMonkey
|
||||
{
|
||||
get { return _selectedMonkey; }
|
||||
set
|
||||
{
|
||||
_selectedMonkey = value;
|
||||
RaisePropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public MonkeysViewModel()
|
||||
{
|
||||
Monkeys = new ObservableCollection<Monkey>
|
||||
|
|
|
@ -5,12 +5,25 @@
|
|||
xmlns:controls="clr-namespace:WrapView.Controls;assembly=WrapView"
|
||||
xmlns:templates="clr-namespace:WrapViewSample.Views.Templates;assembly=WrapViewSample"
|
||||
Title="WrapView">
|
||||
<controls:WrapView
|
||||
ItemsSource="{Binding Monkeys}">
|
||||
<controls:WrapView.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<templates:MonkeyItemTemplate/>
|
||||
</DataTemplate>
|
||||
</controls:WrapView.ItemTemplate>
|
||||
</controls:WrapView>
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<controls:WrapView
|
||||
Grid.Row="0"
|
||||
ItemsSource="{Binding Monkeys}"
|
||||
SelectedItem="{Binding SelectedMonkey, Mode=TwoWay}">
|
||||
<controls:WrapView.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<templates:MonkeyItemTemplate/>
|
||||
</DataTemplate>
|
||||
</controls:WrapView.ItemTemplate>
|
||||
</controls:WrapView>
|
||||
<Label
|
||||
Grid.Row="1"
|
||||
Text="{Binding SelectedMonkey.Name}"
|
||||
TextColor="Black"
|
||||
FontSize="Medium"/>
|
||||
</Grid>
|
||||
</ContentPage>
|
|
@ -1,5 +1,4 @@
|
|||
using WrapView;
|
||||
using WrapViewSample.ViewModels;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace WrapViewSample.Views
|
||||
|
@ -16,17 +15,5 @@ namespace WrapViewSample.Views
|
|||
|
||||
BindingContext = App.Locator.MonkeysViewModel;
|
||||
}
|
||||
|
||||
protected override void OnAppearing()
|
||||
{
|
||||
var viewModel = BindingContext as MonkeysViewModel;
|
||||
if (viewModel != null) viewModel.OnAppearing(Parameter);
|
||||
}
|
||||
|
||||
protected override void OnDisappearing()
|
||||
{
|
||||
var viewModel = BindingContext as MonkeysViewModel;
|
||||
if (viewModel != null) viewModel.OnDisappearing();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,18 +1,84 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace WrapView.Controls
|
||||
{
|
||||
public class WrapPanel : Layout<View>
|
||||
public class WrapView : Layout<View>
|
||||
{
|
||||
public IEnumerable ItemsSource
|
||||
{
|
||||
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
|
||||
set { SetValue(ItemsSourceProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly BindableProperty ItemsSourceProperty =
|
||||
BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(WrapView), default(IEnumerable<object>),
|
||||
BindingMode.TwoWay, propertyChanged: ItemsSourceChanged);
|
||||
|
||||
public object SelectedItem
|
||||
{
|
||||
get { return (object)GetValue(SelectedItemProperty); }
|
||||
set { SetValue(SelectedItemProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly BindableProperty SelectedItemProperty =
|
||||
BindableProperty.Create("SelectedItem", typeof(object), typeof(WrapView), default(object),
|
||||
BindingMode.TwoWay, null);
|
||||
|
||||
public DataTemplate ItemTemplate
|
||||
{
|
||||
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
|
||||
set { SetValue(ItemTemplateProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly BindableProperty ItemTemplateProperty =
|
||||
BindableProperty.Create("ItemTemplate", typeof(DataTemplate), typeof(WrapView),
|
||||
default(DataTemplate));
|
||||
|
||||
private static void ItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
|
||||
{
|
||||
var itemsLayout = (WrapView)bindable;
|
||||
itemsLayout.SetItemsSource();
|
||||
}
|
||||
|
||||
protected virtual void SetItemsSource()
|
||||
{
|
||||
if (ItemsSource == null)
|
||||
return;
|
||||
|
||||
foreach (var item in ItemsSource)
|
||||
Children.Add(GetItem(item));
|
||||
}
|
||||
|
||||
protected virtual View GetItem(object item)
|
||||
{
|
||||
var content = ItemTemplate.CreateContent();
|
||||
var view = content as View;
|
||||
|
||||
var tapEvent = new TapGestureRecognizer();
|
||||
tapEvent.Tapped += (c, r) =>
|
||||
{
|
||||
SelectedItem = item;
|
||||
};
|
||||
|
||||
if (view == null)
|
||||
return null;
|
||||
|
||||
view.GestureRecognizers.Add(tapEvent);
|
||||
view.BindingContext = item;
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
protected override void LayoutChildren(double x, double y, double width, double height)
|
||||
{
|
||||
double rowHeight = 0;
|
||||
double yPos = y, xPos = x;
|
||||
|
||||
foreach (var child in Children)
|
||||
foreach (var child in Children.Where(c => c.IsVisible))
|
||||
{
|
||||
var request = child.GetSizeRequest(width, height);
|
||||
|
||||
|
@ -31,76 +97,6 @@ namespace WrapView.Controls
|
|||
LayoutChildIntoBoundingRegion(child, region);
|
||||
xPos += region.Width;
|
||||
}
|
||||
|
||||
HeightRequest = yPos;
|
||||
}
|
||||
}
|
||||
|
||||
public class WrapView : Grid
|
||||
{
|
||||
protected readonly ScrollView ScrollView;
|
||||
protected readonly WrapPanel WrapPanel;
|
||||
|
||||
public IEnumerable ItemsSource
|
||||
{
|
||||
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
|
||||
set { SetValue(ItemsSourceProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly BindableProperty ItemsSourceProperty =
|
||||
BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(WrapView), default(IEnumerable<object>),
|
||||
BindingMode.TwoWay, propertyChanged: ItemsSourceChanged);
|
||||
|
||||
public DataTemplate ItemTemplate
|
||||
{
|
||||
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
|
||||
set { SetValue(ItemTemplateProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly BindableProperty ItemTemplateProperty =
|
||||
BindableProperty.Create("ItemTemplate", typeof(DataTemplate), typeof(WrapView), default(DataTemplate));
|
||||
|
||||
public WrapView()
|
||||
{
|
||||
ScrollView = new ScrollView
|
||||
{
|
||||
Orientation = ScrollOrientation.Vertical
|
||||
};
|
||||
|
||||
WrapPanel = new WrapPanel();
|
||||
|
||||
ScrollView.Content = WrapPanel;
|
||||
Children.Add(ScrollView);
|
||||
}
|
||||
|
||||
private static void ItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
|
||||
{
|
||||
var itemsLayout = (WrapView)bindable;
|
||||
itemsLayout.SetItemsSource();
|
||||
}
|
||||
|
||||
protected virtual void SetItemsSource()
|
||||
{
|
||||
WrapPanel.Children.Clear();
|
||||
|
||||
if (ItemsSource == null)
|
||||
return;
|
||||
|
||||
foreach (var item in ItemsSource)
|
||||
WrapPanel.Children.Add(GetItem(item));
|
||||
}
|
||||
|
||||
protected virtual View GetItem(object item)
|
||||
{
|
||||
var content = ItemTemplate.CreateContent();
|
||||
var view = content as View;
|
||||
|
||||
if (view == null)
|
||||
return null;
|
||||
|
||||
view.BindingContext = item;
|
||||
|
||||
return view;
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче