Files
aistudio-wpf-diagram/AIStudio.Wpf.DiagramDesigner/StyleSelectors/DesignerItemsControlItemStyleSelector.cs

98 lines
3.1 KiB
C#
Raw Normal View History

2021-07-23 09:42:22 +08:00
using System;
using System.Collections.Concurrent;
2021-07-23 09:42:22 +08:00
using System.Windows;
using System.Windows.Controls;
2022-10-28 22:45:39 +08:00
namespace AIStudio.Wpf.DiagramDesigner
2021-07-23 09:42:22 +08:00
{
public class DesignerItemsControlItemStyleSelector : StyleSelector
{
static DesignerItemsControlItemStyleSelector()
{
Instance = new DesignerItemsControlItemStyleSelector();
}
public static DesignerItemsControlItemStyleSelector Instance
{
get;
private set;
}
private static ConcurrentDictionary<Type, string> _styles = new ConcurrentDictionary<Type, string>();
public void RegisterDesignerItemControlItemStyle(Type type, string resourceName)
{
if (_styles.ContainsKey(type))
{
throw new InvalidOperationException($"{type}-{resourceName} already exists");
}
_styles.TryAdd(type, resourceName);
}
public void RegisterDesignerItemControlItemStyle(Type type, string resourceName, bool isCover)
{
if (_styles.ContainsKey(type))
{
_styles[type] = resourceName;
}
_styles.TryAdd(type, resourceName);
}
2021-07-23 09:42:22 +08:00
public override Style SelectStyle(object item, DependencyObject container)
{
ItemsControl itemsControl = ItemsControl.ItemsControlFromItemContainer(container);
if (itemsControl == null)
throw new InvalidOperationException("DesignerItemsControlItemStyleSelector : Could not find ItemsControl");
if (!_styles.IsNullOrEmpty())
{
var type = item.GetType();
if (_styles.ContainsKey(type))
{
return (Style)itemsControl.FindResource(_styles[type]);
}
foreach (var styleItem in _styles)
{
if (type.IsSubclassOf(styleItem.Key))
{
return (Style)itemsControl.FindResource(styleItem.Value);
}
if (type.IsAssignableFrom(styleItem.Key))
{
return (Style)itemsControl.FindResource(styleItem.Value);
}
}
}
2021-07-23 09:42:22 +08:00
if (item is GifImageItemViewModel)
{
return (Style)itemsControl.FindResource("gifimageItemStyle");
}
if (item is LogicalGateItemViewModelBase)
{
return (Style)itemsControl.FindResource("logicalItemStyle");
}
if (item is BlockDesignerItemViewModel)
{
return (Style)itemsControl.FindResource("blockItemStyle");
}
2021-07-23 09:42:22 +08:00
if (item is DesignerItemViewModelBase)
{
return (Style)itemsControl.FindResource("designerItemStyle");
}
2023-01-24 17:53:04 +08:00
if (item is ConnectionViewModel)
2021-07-23 09:42:22 +08:00
{
return (Style)itemsControl.FindResource("connectorItemStyle");
}
2021-07-23 09:42:22 +08:00
return null;
}
}
2021-07-23 09:42:22 +08:00
}