定义模版选择器

This commit is contained in:
于智纯
2025-09-17 22:34:54 +08:00
parent 9106a8d769
commit a7379e3eff
5 changed files with 84 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Deedy.Activity
{
public class ActionTemplateCollection : ObservableCollection<DataTemplate>
{
public ActionTemplateCollection() { }
public bool TryGetTemplate(Type type, out DataTemplate template)
{
DataTemplate dataTemplate = new DataTemplate(typeof(IActionElement));
bool result = false;
foreach (var item in this)
{
if (type.Equals(item.DataType))
{
dataTemplate = item;
result = true;
break;
}
}
template = dataTemplate;
return result;
}
}
}

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
namespace Deedy.Activity
{
[ContentProperty("ActionTemplates")]
[Localizability(LocalizationCategory.Ignore)]
public class ActionTemplateSelector : DataTemplateSelector
{
[AllowNull]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ActionTemplateCollection ActionTemplates { get; internal set; } = new ActionTemplateCollection();
[AllowNull]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Uri Source { get; set; }
private bool IsLoaded = false;
private void LoadFromSource()
{
if (IsLoaded || Source == null) return;
//TODO从Source中加载DataTemplate要求DataType必须继承IActionElement接口ActionTemplates中设置的模版高于Source中的模版
IsLoaded = true;
}
public ActionTemplateSelector() { }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
this.LoadFromSource();
return base.SelectTemplate(item, container);
}
}
}

View File

@@ -0,0 +1,4 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</ResourceDictionary>

View File

@@ -0,0 +1,9 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Deedy.Activity">
<local:ActionTemplateSelector x:Key="AuxTemplateSelector">
<DataTemplate DataType="{x:Type local:BasalAction}">
</DataTemplate>
</local:ActionTemplateSelector>
</ResourceDictionary>