|
27 | 27 | using System.Windows.Media; |
28 | 28 | using System.Windows.Media.Imaging; |
29 | 29 | using System.Windows.Xps.Serialization; |
| 30 | +using ICSharpCode.WpfDesign.Designer.Xaml; |
30 | 31 |
|
31 | 32 | namespace ICSharpCode.WpfDesign.Designer |
32 | 33 | { |
@@ -137,7 +138,7 @@ internal static void CreateVisualTree(this UIElement element) |
137 | 138 | catch (Exception) |
138 | 139 | { } |
139 | 140 | } |
140 | | - |
| 141 | + |
141 | 142 | internal static Size GetDefaultSize(DesignItem createdItem) |
142 | 143 | { |
143 | 144 | CreateVisualTree(createdItem.View); |
@@ -198,5 +199,174 @@ public static void Resize(DesignItem item, double newWidth, double newHeight) |
198 | 199 | item.Properties.GetProperty(FrameworkElement.HeightProperty).SetValue(newHeight); |
199 | 200 | } |
200 | 201 | } |
| 202 | + |
| 203 | + |
| 204 | + private class ItemPos |
| 205 | + { |
| 206 | + public HorizontalAlignment HorizontalAlignment{ get; set; } |
| 207 | + |
| 208 | + public VerticalAlignment VerticalAlignment{ get; set; } |
| 209 | + |
| 210 | + public double Xmin { get; set; } |
| 211 | + |
| 212 | + public double Xmax { get; set; } |
| 213 | + |
| 214 | + public double Ymin { get; set; } |
| 215 | + |
| 216 | + public double Ymax { get; set; } |
| 217 | + |
| 218 | + public DesignItem DesignItem { get; set; } |
| 219 | + } |
| 220 | + |
| 221 | + public static void WrapItemsNewContainer(IEnumerable<DesignItem> items, Type containerType) |
| 222 | + { |
| 223 | + var collection = items; |
| 224 | + |
| 225 | + var _context = collection.First().Context as XamlDesignContext; |
| 226 | + |
| 227 | + var oldContainer = collection.First().Parent; |
| 228 | + |
| 229 | + if (collection.Any(x => x.Parent != oldContainer)) |
| 230 | + return; |
| 231 | + |
| 232 | + var newInstance = Activator.CreateInstance(containerType); |
| 233 | + DesignItem newPanel = _context.Services.Component.RegisterComponentForDesigner(newInstance); |
| 234 | + var changeGroup = newPanel.OpenGroup("Wrap in Container"); |
| 235 | + |
| 236 | + List<ItemPos> itemList = new List<ItemPos>(); |
| 237 | + |
| 238 | + foreach (var item in collection) { |
| 239 | + |
| 240 | + var itemPos = new ItemPos(){ DesignItem = item }; |
| 241 | + itemList.Add(itemPos); |
| 242 | + |
| 243 | + if (oldContainer.Component is Canvas) { |
| 244 | + var canvas = oldContainer.View as Canvas; |
| 245 | + |
| 246 | + if (item.Properties.GetAttachedProperty(Canvas.RightProperty) != null && item.Properties.GetAttachedProperty(Canvas.RightProperty).IsSet) { |
| 247 | + itemPos.HorizontalAlignment = HorizontalAlignment.Right; |
| 248 | + itemPos.Xmax = canvas.ActualWidth - (double)item.Properties.GetAttachedProperty(Canvas.RightProperty).ValueOnInstance; |
| 249 | + itemPos.Xmin = itemPos.Xmax - ((FrameworkElement)item.View).ActualWidth; |
| 250 | + } |
| 251 | + else if (item.Properties.GetAttachedProperty(Canvas.LeftProperty) != null && item.Properties.GetAttachedProperty(Canvas.LeftProperty).IsSet) { |
| 252 | + itemPos.HorizontalAlignment = HorizontalAlignment.Left; |
| 253 | + itemPos.Xmin = (double)item.Properties.GetAttachedProperty(Canvas.LeftProperty).ValueOnInstance; |
| 254 | + itemPos.Xmax = itemPos.Xmin + ((FrameworkElement)item.View).ActualWidth; |
| 255 | + } else { |
| 256 | + itemPos.HorizontalAlignment = HorizontalAlignment.Left; |
| 257 | + itemPos.Xmax = itemPos.Xmin + ((FrameworkElement)item.View).ActualWidth; |
| 258 | + } |
| 259 | + |
| 260 | + if (item.Properties.GetAttachedProperty(Canvas.BottomProperty) != null && item.Properties.GetAttachedProperty(Canvas.BottomProperty).IsSet) { |
| 261 | + itemPos.VerticalAlignment = VerticalAlignment.Bottom; |
| 262 | + itemPos.Ymax = canvas.ActualHeight - (double)item.Properties.GetAttachedProperty(Canvas.BottomProperty).ValueOnInstance; |
| 263 | + itemPos.Ymin = itemPos.Ymax - ((FrameworkElement)item.View).ActualHeight; |
| 264 | + } |
| 265 | + else if (item.Properties.GetAttachedProperty(Canvas.TopProperty) != null && item.Properties.GetAttachedProperty(Canvas.TopProperty).IsSet) { |
| 266 | + itemPos.VerticalAlignment = VerticalAlignment.Top; |
| 267 | + itemPos.Ymin = (double)item.Properties.GetAttachedProperty(Canvas.TopProperty).ValueOnInstance; |
| 268 | + itemPos.Ymax = itemPos.Ymin + ((FrameworkElement)item.View).ActualHeight; |
| 269 | + } else { |
| 270 | + itemPos.VerticalAlignment = VerticalAlignment.Top; |
| 271 | + itemPos.Ymax = itemPos.Ymin + ((FrameworkElement)item.View).ActualHeight; |
| 272 | + } |
| 273 | + |
| 274 | + item.Properties.GetAttachedProperty(Canvas.RightProperty).Reset(); |
| 275 | + item.Properties.GetAttachedProperty(Canvas.LeftProperty).Reset(); |
| 276 | + item.Properties.GetAttachedProperty(Canvas.TopProperty).Reset(); |
| 277 | + item.Properties.GetAttachedProperty(Canvas.BottomProperty).Reset(); |
| 278 | + } else if (oldContainer.Component is Grid) { |
| 279 | + var grid = oldContainer.View as Grid; |
| 280 | + |
| 281 | + if ((HorizontalAlignment)item.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).ValueOnInstance == HorizontalAlignment.Right) { |
| 282 | + itemPos.HorizontalAlignment = HorizontalAlignment.Right; |
| 283 | + itemPos.Xmax = grid.ActualWidth - ((Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance).Right; |
| 284 | + itemPos.Xmin = itemPos.Xmax - ((FrameworkElement)item.View).ActualWidth; |
| 285 | + } else { |
| 286 | + itemPos.HorizontalAlignment = HorizontalAlignment.Left; |
| 287 | + itemPos.Xmin = ((Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance).Left; |
| 288 | + itemPos.Xmax = itemPos.Xmin + ((FrameworkElement)item.View).ActualWidth; |
| 289 | + } |
| 290 | + |
| 291 | + if ((VerticalAlignment)item.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).ValueOnInstance == VerticalAlignment.Bottom) { |
| 292 | + itemPos.VerticalAlignment = VerticalAlignment.Bottom; |
| 293 | + itemPos.Ymax = grid.ActualHeight - ((Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance).Bottom; |
| 294 | + itemPos.Ymin = itemPos.Ymax - ((FrameworkElement)item.View).ActualHeight; |
| 295 | + } else { |
| 296 | + itemPos.VerticalAlignment = VerticalAlignment.Top; |
| 297 | + itemPos.Ymin = ((Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance).Top; |
| 298 | + itemPos.Ymax = itemPos.Ymin + ((FrameworkElement)item.View).ActualHeight; |
| 299 | + } |
| 300 | + |
| 301 | + item.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).Reset(); |
| 302 | + item.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).Reset(); |
| 303 | + item.Properties.GetProperty(FrameworkElement.MarginProperty).Reset(); |
| 304 | + } |
| 305 | + |
| 306 | + var parCol = item.ParentProperty.CollectionElements; |
| 307 | + parCol.Remove(item); |
| 308 | + } |
| 309 | + |
| 310 | + var xmin = itemList.Min(x => x.Xmin); |
| 311 | + var xmax = itemList.Max(x => x.Xmax); |
| 312 | + var ymin = itemList.Min(x => x.Ymin); |
| 313 | + var ymax = itemList.Max(x => x.Ymax); |
| 314 | + |
| 315 | + if (oldContainer.Component is Canvas) { |
| 316 | + newPanel.Properties.GetProperty(FrameworkElement.WidthProperty).SetValue(xmax - xmin); |
| 317 | + newPanel.Properties.GetProperty(FrameworkElement.HeightProperty).SetValue(ymax - ymin); |
| 318 | + newPanel.Properties.GetAttachedProperty(Canvas.LeftProperty).SetValue(xmin); |
| 319 | + newPanel.Properties.GetAttachedProperty(Canvas.TopProperty).SetValue(ymin); |
| 320 | + } else if (oldContainer.Component is Grid) { |
| 321 | + newPanel.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).SetValue(HorizontalAlignment.Left); |
| 322 | + newPanel.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).SetValue(VerticalAlignment.Top); |
| 323 | + newPanel.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(new Thickness(xmin, ymin, 0, 0)); |
| 324 | + newPanel.Properties.GetProperty(FrameworkElement.WidthProperty).SetValue(xmax - xmin); |
| 325 | + newPanel.Properties.GetProperty(FrameworkElement.HeightProperty).SetValue(ymax - ymin); |
| 326 | + } |
| 327 | + |
| 328 | + foreach (var item in itemList) { |
| 329 | + newPanel.ContentProperty.CollectionElements.Add(item.DesignItem); |
| 330 | + |
| 331 | + if (newPanel.Component is Canvas) { |
| 332 | + if (item.HorizontalAlignment == HorizontalAlignment.Right) { |
| 333 | + item.DesignItem.Properties.GetAttachedProperty(Canvas.RightProperty).SetValue(xmax - item.Xmax); |
| 334 | + } else { |
| 335 | + item.DesignItem.Properties.GetAttachedProperty(Canvas.LeftProperty).SetValue(item.Xmin - xmin); |
| 336 | + } |
| 337 | + |
| 338 | + if (item.VerticalAlignment == VerticalAlignment.Bottom) { |
| 339 | + item.DesignItem.Properties.GetAttachedProperty(Canvas.BottomProperty).SetValue(ymax - item.Ymax); |
| 340 | + } else { |
| 341 | + item.DesignItem.Properties.GetAttachedProperty(Canvas.TopProperty).SetValue(item.Ymin - ymin); |
| 342 | + } |
| 343 | + } else if (newPanel.Component is Grid) { |
| 344 | + Thickness thickness = new Thickness(0); |
| 345 | + if (item.HorizontalAlignment == HorizontalAlignment.Right) { |
| 346 | + item.DesignItem.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).SetValue(HorizontalAlignment.Right); |
| 347 | + thickness.Right = xmax - item.Xmax; |
| 348 | + } else { |
| 349 | + item.DesignItem.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).SetValue(HorizontalAlignment.Left); |
| 350 | + thickness.Left = item.Xmin - xmin; |
| 351 | + } |
| 352 | + |
| 353 | + if (item.VerticalAlignment == VerticalAlignment.Bottom) { |
| 354 | + item.DesignItem.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).SetValue(VerticalAlignment.Bottom); |
| 355 | + thickness.Bottom = ymax - item.Ymax; |
| 356 | + } else { |
| 357 | + item.DesignItem.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).SetValue(VerticalAlignment.Top); |
| 358 | + thickness.Top = item.Ymin - ymin; |
| 359 | + } |
| 360 | + |
| 361 | + item.DesignItem.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(thickness); |
| 362 | + } |
| 363 | + } |
| 364 | + |
| 365 | + oldContainer.ContentProperty.CollectionElements.Add(newPanel); |
| 366 | + |
| 367 | + changeGroup.Commit(); |
| 368 | + |
| 369 | + _context.Services.Selection.SetSelectedComponents(new []{ newPanel }); |
| 370 | + } |
201 | 371 | } |
202 | 372 | } |
0 commit comments