add Element in canvas

This commit is contained in:
w-mj committed 2019-04-29 10:51:53 +08:00
1 parent f4062dc5a6
commit c80e895d4a
2 files changed
+30 -46

No files matched your search

+27 -37
View File
@@ -19,50 +19,40 @@ namespace FileManager_UWP.Controls {
public class LabelListControl: ItemsControl { public class LabelListControl: ItemsControl {
public LabelListControl() { public LabelListControl() {
this.DefaultStyleKey = typeof(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) { private void ItemsSource_CollectionChanged(object sender,
Debug.WriteLine("Add ELEMENT"); System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
OnApplyTemplate(); TextBlock textBlock = (TextBlock)GetTemplateChild("LabelTextBlock");
} textBlock.Text = "测试自定义控件\n";
//listView.ItemsSource = ItemsSource;
var itemsSource = ItemsSource as ObservableCollection<string>;
textBlock.Text += itemsSource.Count().ToString();
public new ObservableCollection<string> ItemsSource { Canvas labelListCanvas = (Canvas)GetTemplateChild("LabelListCanvas");
get { return (ObservableCollection<string>)GetValue(ItemsControl.ItemsSourceProperty); } //labelListCanvas.Children.Count;
set { SetValue(ItemsControl.ItemsSourceProperty, value); } labelListCanvas.Children.Clear();
} foreach (string label in itemsSource) {
Button b = new Button();
public new static readonly DependencyProperty ItemsSourceProperty = b.Content = label;
DependencyProperty.Register( b.SetValue(Canvas.LeftProperty, labelListCanvas.Children.Count * 10);
"ItemsSource", typeof(ObservableCollection<string>), typeof(LabelListControl), labelListCanvas.Children.Add(b);
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();
} }
} }
private void update() {
}
private void init() {
var itemsSource = ItemsSource as ObservableCollection<string>;
itemsSource.CollectionChanged += ItemsSource_CollectionChanged;
TextBlock textBlock = (TextBlock)GetTemplateChild("LabelTextBlock");
textBlock.Text = "测试自定义控件\nINIT";
}
protected override void OnApplyTemplate() { protected override void OnApplyTemplate() {
TextBlock textBlock = (TextBlock)GetTemplateChild("LabelTextBlock"); init();
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";
}
} }
} }
} }
@@ -9,15 +9,9 @@
<ControlTemplate TargetType="local:LabelListControl"> <ControlTemplate TargetType="local:LabelListControl">
<StackPanel> <StackPanel>
<TextBlock Name="LabelTextBlock"></TextBlock> <TextBlock Name="LabelTextBlock"></TextBlock>
<!--<ListView Name="LabelListView"> <Canvas Name="LabelListCanvas">
<ListView.ItemTemplate>
<DataTemplate> </Canvas>
<Grid>
<TextBlock Text="{Binding}"></TextBlock>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>-->
</StackPanel> </StackPanel>
</ControlTemplate> </ControlTemplate>
</Setter.Value> </Setter.Value>