private void AddItem(UIElement view) { if (Template == null) return; var container = Template.FindName(ItemsPanelTemplatePartName, this) as Grid; var main = Template.FindName(MainPanelTemplatePartName, this) as Border; var widgets = Template.FindName(WidgetsPanelTemplatePartName, this) as UniformGrid; if (main != null && view != null) { this.SetValue(ViewName.TitleProperty, view.GetValue(ViewName.TitleProperty)); ScaleBoxCC widgetToRemove = widgets.Children.Cast<ScaleBoxCC>().FirstOrDefault(widget => widget.Child == view); if (widgetToRemove != null) { widgetToRemove.Child = null; widgets.Children.Remove(widgetToRemove); } var oldview = main.Child as FrameworkElement; main.Child = view; (view as FrameworkElement).Height = Double.NaN; (view as FrameworkElement).Width = Double.NaN; if (oldview != null) { var newWidget = new ScaleBoxCC(oldview); widgets.Children.Add(newWidget); newWidget.Activate += Widget_Activate; newWidget.Closed += Widget_Close; } Application.Current.Dispatcher.BeginInvoke(new Action(() => this.SetWidgetsArea()), System.Windows.Threading.DispatcherPriority.ApplicationIdle); //if (widgets.Children.Count > 0 && container.ColumnDefinitions[0].Width.Value < 2) //{ // container.ColumnDefinitions[0].Width = new GridLength(container.ActualWidth / 4); //} //if (widgets.Children.Count > widgets.Columns * widgets.Rows) //{ // var actualCellHeight = widgets.ActualHeight / widgets.Columns; // var actualCellWidth = widgets.ActualWidth / widgets.Rows; // var previewActualCellHeight = widgets.ActualHeight / (widgets.Rows + 1); // var previewActualCellWidth = widgets.ActualWidth / (widgets.Columns + 1); // if (Math.Abs(previewActualCellHeight - actualCellWidth) < Math.Abs(previewActualCellWidth - actualCellHeight)) // { // widgets.Rows++; // } // else // { // widgets.Columns++; // } //} } }
private void AddItem(UIElement view) { if (Template == null) { return; } var container = Template.FindName(ItemsPanelTemplatePartName, this) as Grid; var main = Template.FindName(MainPanelTemplatePartName, this) as Border; var widgets = Template.FindName(WidgetsPanelTemplatePartName, this) as UniformGrid; if (main != null && view != null) { this.SetValue(ViewName.TitleProperty, view.GetValue(ViewName.TitleProperty)); ScaleBoxCC widgetToRemove = widgets.Children.Cast <ScaleBoxCC>().FirstOrDefault(widget => widget.Child == view); if (widgetToRemove != null) { widgetToRemove.Child = null; widgets.Children.Remove(widgetToRemove); } var oldview = main.Child as FrameworkElement; main.Child = view; (view as FrameworkElement).Height = Double.NaN; (view as FrameworkElement).Width = Double.NaN; if (oldview != null) { var newWidget = new ScaleBoxCC(oldview); widgets.Children.Add(newWidget); newWidget.Activate += Widget_Activate; newWidget.Closed += Widget_Close; } Application.Current.Dispatcher.BeginInvoke(new Action(() => this.SetWidgetsArea()), System.Windows.Threading.DispatcherPriority.ApplicationIdle); //if (widgets.Children.Count > 0 && container.ColumnDefinitions[0].Width.Value < 2) //{ // container.ColumnDefinitions[0].Width = new GridLength(container.ActualWidth / 4); //} //if (widgets.Children.Count > widgets.Columns * widgets.Rows) //{ // var actualCellHeight = widgets.ActualHeight / widgets.Columns; // var actualCellWidth = widgets.ActualWidth / widgets.Rows; // var previewActualCellHeight = widgets.ActualHeight / (widgets.Rows + 1); // var previewActualCellWidth = widgets.ActualWidth / (widgets.Columns + 1); // if (Math.Abs(previewActualCellHeight - actualCellWidth) < Math.Abs(previewActualCellWidth - actualCellHeight)) // { // widgets.Rows++; // } // else // { // widgets.Columns++; // } //} } }