bind observable collection on labelListView

This commit is contained in:
w-mj committed 2019-04-28 21:55:02 +08:00
1 parent 2293d207c5
commit f4062dc5a6
4 files changed
+77 -5

No files matched your search

+46 -2
View File
@@ -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<string>();
//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<string> ItemsSource {
get { return (ObservableCollection<string>)GetValue(ItemsControl.ItemsSourceProperty); }
set { SetValue(ItemsControl.ItemsSourceProperty, value); }
}
public new static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register(
"ItemsSource", typeof(ObservableCollection<string>), typeof(LabelListControl),
new PropertyMetadata(null, OnItemsChanged));
private static void OnItemsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) {
Debug.WriteLine("Add Element.");
LabelListControl target = obj as LabelListControl;
ObservableCollection<string> oldValue = (ObservableCollection<string>)args.OldValue;
ObservableCollection<string> newValue = (ObservableCollection<string>)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<string>).Count().ToString();
} else {
textBlock.Text += "0";
}
}
}
}