private static void SetItemSource(DependencyObject d, DependencyPropertyChangedEventArgs e) { AdaGrid adaGrid = d as AdaGrid; adaGrid.Items = e.NewValue as ObservableCollection <PictureCard>; ReSetItems(adaGrid); adaGrid.Items.CollectionChanged += adaGrid.Items_CollectionChanged; }
static void ReSetItems(AdaGrid adaGrid) { adaGrid.Grid_Main.Children.Clear(); if (adaGrid.Items == null || adaGrid.Items.Count == 0) { return; } //断开父元素 foreach (var temp in adaGrid.Items) { if (temp.Parent != null) { StackPanel p = temp.Parent as StackPanel; p.Children.Clear(); p = null; } } double grdiW = adaGrid.ActualWidth; int columnCount = (int)(grdiW / (adaGrid.ItemWidth + 10)); for (int i = 0; i < adaGrid.Items.Count; i++) { StackPanel stackPanel = new StackPanel() { Orientation = Orientation.Horizontal }; for (int j = 0; j < columnCount && i < adaGrid.Items.Count; j++, i++) { stackPanel.Children.Add(adaGrid.Items[i]); } i--; adaGrid.Grid_Main.Children.Add(stackPanel); //} //if (adaGrid.Grid_Main.Children.Count % columnCount == 0) // adaGrid.Grid_Main.Children.Add(); } }