- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我创建了一个 DataTemplateSelector,它使用一组已知接口(interface)进行初始化。如果传递到选择器的项目实现了这些接口(interface)之一,则返回关联的数据模板。
首先,这是有问题的 ICategory 接口(interface)...
public interface ICategory
{
ICategory ParentCategory { get; set; }
string Name { get; set; }
ICategoryCollection Subcategories { get; }
}
这里是 DataTemplateSelector,它基于基类或接口(interface)进行匹配,而不仅仅是特定的具体类......
[ContentProperty("BaseTypeMappings")]
public class SubclassedTypeTemplateSelector : DataTemplateSelector
{
private delegate object TryFindResourceDelegate(object key);
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var frameworkElement = container as FrameworkElement;
foreach(var baseTypeMapping in BaseTypeMappings)
{
// Check if the item is an instance of, a subclass of,
// or implements the interface specified in BaseType
if(baseTypeMapping.BaseType.IsInstanceOfType(item))
{
// Create a key based on the BaseType, (not item.DataType as usual)
var resourceKey = new DataTemplateKey(baseTypeMapping.BaseType);
// Get TryFindResource method from either the FrameworkElement,
// or from the application
var tryFindResource = (frameworkElement != null)
? (TryFindResourceDelegate)frameworkElement.TryFindResource
: Application.Current.TryFindResource;
// Use the TryFindResource delegate from above to try finding
// the resource based on the resource key
var dataTemplate = (DataTemplate)tryFindResource(resourceKey);
dataTemplate.DataType = item.GetType();
if(dataTemplate != null)
return dataTemplate;
}
}
var defaultTemplate = DefaultDataTemplate ?? base.SelectTemplate(item, container);
return defaultTemplate;
}
public DataTemplate DefaultDataTemplate { get; set; }
public Collection<BaseTypeMapping> BaseTypeMappings { get; } = new Collection<BaseTypeMapping>();
}
public class BaseTypeMapping
{
public Type BaseType { get; set; }
}
这是它在资源中的设置方式以及相应的 HierarchicalDataTemplate with DataType = ICategory...
<HierarchicalDataTemplate DataType="{x:Type model:ICategory}"
ItemsSource="{Binding Subcategories}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
<is:SubclassedTypeTemplateSelector x:Key="SubclassedTypeTemplateSelector">
<!--<is:BaseTypeMapping BaseType="{x:Type model:ICategory}" />-->
</is:SubclassedTypeTemplateSelector>
最后,这是一个使用它的 TreeView...
<TreeView x:Name="MainTreeView"
ItemsSource="{Binding Categories}"
ItemTemplateSelector="{StaticResource SubclassedTypeTemplateSelector}" />
我已经对其进行了调试,并且可以确认正确的数据模板正在返回到 TreeView,这既是单步执行代码的预期,也是因为 TreeView 正在根据 HierarchicalDataTemplate 上的 ItemSource 绑定(bind)正确加载子类别。所有这些都按预期工作。
不起作用的是模板本身的内容。如您所见,模板只是应该显示类别的名称,但它只是呈现原始对象,就好像它直接放在 ContentPresenter 中而没有任何模板一样。您在 UI 中看到的都是 ToString 的结果。模板的内容被完全忽略。
我唯一能想到的是它不起作用,因为我正在使用 DataType 的接口(interface),但同样,子项的 ItemsSource 的绑定(bind)确实起作用,所以我在这里有点难过。
值得注意的是:作为测试,我基于具体类型(即类别而不仅仅是 ICategory)创建了第二个 DataTemplate,当我这样做时,它按预期工作。问题是具体类型位于不应被 UI 引用的程序集中。这就是我们首先使用接口(interface)的全部原因。
*NOTE: I have also tried changing the way I look up the template by using a Key instead of setting the DataType property. In that case, just as before, the selector still finds the same resource, but it still doesn't work!
Ironically however, if I use that same key to set the ItemTemplate of the TreeView directly via a StaticResource binding, then it does work, meaning it only doesn't work when I return the template from the selector and does not appear related to whether DataType is set or not.*
最佳答案
What doesn't work is the contents of the template itself
这是因为您在 XAML 标记中定义的模板未被应用,因为 DataType 属性设置为接口(interface)类型。正如@Manfred Radlwimmer 所说,这是设计使然:https://social.msdn.microsoft.com/Forums/vstudio/en-US/1e774a24-0deb-4acd-a719-32abd847041d/data-templates-and-interfaces?forum=wpf .从 DataTemplateSelector 返回这样的模板并不能使它像您已经发现的那样工作。
但是如果您使用 DataTemplateSelector 选择合适的数据模板,您可以从数据模板中删除 DataType 属性,并为每个模板提供一个唯一的 x:Key:
<HierarchicalDataTemplate x:Key="ICategory" ItemsSource="{Binding Subcategories}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
然后您应该能够使用此 key 解析资源,例如:
var baseTypeName = "ICategory";
var dataTemplate = (DataTemplate)tryFindResource("baseTypeName");
关于c# - 当从 DataTemplateSelector 显式返回 DataTemplate 时,为什么 DataTemplate 不能绑定(bind)到接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41714918/
我是一名优秀的程序员,十分优秀!