From f4062dc5a6f01edf040aaf2766eb3ecd7635dfa7 Mon Sep 17 00:00:00 2001 From: w-mj Date: Sun, 28 Apr 2019 21:55:02 +0800 Subject: [PATCH] bind observable collection on labelListView --- FileManager_UWP/Controls/LabelListControl.cs | 48 ++++++++++++++++++- .../Controls/LabelListControl.xaml | 13 ++++- FileManager_UWP/View/TestLabelView.xaml | 11 ++++- FileManager_UWP/View/TestLabelView.xaml.cs | 10 ++++ 4 files changed, 77 insertions(+), 5 deletions(-) diff --git a/FileManager_UWP/Controls/LabelListControl.cs b/FileManager_UWP/Controls/LabelListControl.cs index 8927ab6..de59b87 100644 --- a/FileManager_UWP/Controls/LabelListControl.cs +++ b/FileManager_UWP/Controls/LabelListControl.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.UI.Xaml; @@ -7,18 +9,60 @@ using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Documents; using Windows.UI.Xaml.Input; +using Windows.UI.Xaml.Markup; using Windows.UI.Xaml.Media; // The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235 namespace FileManager_UWP.Controls { - public sealed class LabelListControl: Control { + + public class LabelListControl: ItemsControl { public LabelListControl() { this.DefaultStyleKey = typeof(LabelListControl); + //var itemSource = new ObservableCollection(); + //itemSource.CollectionChanged += ItemsSource_CollectionChanged; + //ItemsSource = itemSource; + //Debug.WriteLine(itemSource.ToString()); + } + + private void ItemsSource_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { + Debug.WriteLine("Add ELEMENT"); + OnApplyTemplate(); + } + + public new ObservableCollection ItemsSource { + get { return (ObservableCollection)GetValue(ItemsControl.ItemsSourceProperty); } + set { SetValue(ItemsControl.ItemsSourceProperty, value); } + } + + public new static readonly DependencyProperty ItemsSourceProperty = + DependencyProperty.Register( + "ItemsSource", typeof(ObservableCollection), typeof(LabelListControl), + new PropertyMetadata(null, OnItemsChanged)); + + private static void OnItemsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { + Debug.WriteLine("Add Element."); + LabelListControl target = obj as LabelListControl; + ObservableCollection oldValue = (ObservableCollection)args.OldValue; + ObservableCollection newValue = (ObservableCollection)args.NewValue; + if (oldValue != newValue) { + target.ItemsSource = newValue; + target.OnApplyTemplate(); + } } protected override void OnApplyTemplate() { - ((TextBlock)GetTemplateChild("LabelTextBlock")).Text = "测试自定义控件"; + TextBlock textBlock = (TextBlock)GetTemplateChild("LabelTextBlock"); + textBlock.Text = "测试自定义控件\n"; + ListView listView = (ListView)GetTemplateChild("LabelListView"); + //listView.ItemsSource = ItemsSource; + ItemsSource.CollectionChanged += ItemsSource_CollectionChanged; + Debug.WriteLine(ItemsSource.ToString()); + if (ItemsSource != null) { + textBlock.Text += (ItemsSource as ICollection).Count().ToString(); + } else { + textBlock.Text += "0"; + } } } } diff --git a/FileManager_UWP/Controls/LabelListControl.xaml b/FileManager_UWP/Controls/LabelListControl.xaml index 8708748..b3db606 100644 --- a/FileManager_UWP/Controls/LabelListControl.xaml +++ b/FileManager_UWP/Controls/LabelListControl.xaml @@ -7,9 +7,18 @@ - + - + + diff --git a/FileManager_UWP/View/TestLabelView.xaml b/FileManager_UWP/View/TestLabelView.xaml index 27f4880..c8087c3 100644 --- a/FileManager_UWP/View/TestLabelView.xaml +++ b/FileManager_UWP/View/TestLabelView.xaml @@ -13,6 +13,15 @@ - + + + + diff --git a/FileManager_UWP/View/TestLabelView.xaml.cs b/FileManager_UWP/View/TestLabelView.xaml.cs index 2900d54..5dd6255 100644 --- a/FileManager_UWP/View/TestLabelView.xaml.cs +++ b/FileManager_UWP/View/TestLabelView.xaml.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; @@ -20,8 +22,16 @@ namespace FileManager_UWP.View { /// An empty page that can be used on its own or navigated to within a Frame. /// public sealed partial class TestLabelView: Page { + public ObservableCollection Labels = new ObservableCollection{ "miao", "ddd" }; + public TestLabelView() { this.InitializeComponent(); + Debug.WriteLine(Labels.ToString()); + } + + private void Button_Click(object sender, RoutedEventArgs e) { + Labels.Add("KKK"); + Debug.WriteLine(Labels.Count.ToString()); } } }