如何以编程方式检索用于UI层次结构中特定元素的DataTemplate?

时间:2013-08-30 19:05:12

标签: c# wpf datatemplate code-behind

我们需要在代码中确定,在给定特定数据类型和元素的情况下,将自动应用于绑定元素的模板。

我们不是在寻找一个DataTemplateSelector,因为它用于告诉 UI基于自定义逻辑用于给定对象的模板。我们改为询问 UI 用于给定数据类型和UI元素的模板。

换句话说,我们正在根据窗口资源部分中定义的模板查找WPF应用的内容,该模板可以被该窗口上的控件资源覆盖,可以通过显式设置覆盖DataTemplate或直接在该元素上提供DataTemplateSelector。

另外,我们尝试了SelectTemplate的默认实现,但是返回null,所以我们也不能去那条路。

测试是在UI中的任何地方询问没有数据模板或选择器的元素'你将如何显示这个值?'并希望它将返回一个DataTemplate,其中包含TextBlock的定义,并将text属性设置为该对象上的ToString方法,这是默认情况下显示的内容。

3 个答案:

答案 0 :(得分:1)

托马斯·莱维斯克未经考验的解决方案并不适合我,但提供了一个很好的起点。在我们的案例中,"容器"参数并不总是在可视树中,所以首先我们走向逻辑树,直到找到视觉。结合MarqueIV的出色建议,可以得到一个相当简单的解决方案。

以下代码适用于我的制作。你的旅费可能会改变。 :)

public static DataTemplate FindTemplateForType(Type dataType, DependencyObject container)
{
    var frameworkElement = container as FrameworkElement;
    if (frameworkElement != null)
    {
        DataTemplateKey key = new DataTemplateKey(dataType);
        var template = frameworkElement.FindResource(key) as DataTemplate;
        if (template != null)
            return template;
    }

    if (!(container is Visual || container is Visual3D))
    {
        container = FindClosestVisualParent(container);
        return FindTemplateForType(dataType, container);
    }
    else
    {
        var parent = VisualTreeHelper.GetParent(container);
        if (parent != null)
            return FindTemplateForType(dataType, parent);
        else
            return FindTemplateForType(dataType, Application.Current.Windows[0]);
    }
}

public static DependencyObject FindClosestVisualParent(DependencyObject initial)
{
    DependencyObject current = initial;
    bool found = false;

    while (!found)
    {
        if (current is Visual || current is Visual3D)
        {
            found = true;
        }
        else
        {
            current = LogicalTreeHelper.GetParent(current);
        }
    }

    return current;
}

答案 1 :(得分:0)

我猜你可以尝试手动重现WPF用于查找相应DataTemplate的逻辑,方法是向上查看可视树并查找具有相应密钥的资源。这是一个可能的实现(未经测试):

static DataTemplate FindTemplateForType(Type dataType, DependencyObject container)
{
    var frameworkElement = container as FrameworkElement;
    if (frameworkElement != null)
    {
        var template = FindTemplateForType(dataType, frameworkElement.Resources);
        if (template != null)
            return template;
    }

    var parent = VisualTreeHelper.GetParent(container);
    if (parent != null)
        return FindTemplateForType(dataType, parent);
    else
        return FindTemplateForType(dataType, Application.Current.Resources);
}

static DataTemplate FindTemplateForType(Type dataType, ResourceDictionary resources)
{
    var entries =
        from DictionaryEntry e in resources
        where e.Key is Type && e.Value is DataTemplate
        let type = (Type)e.Key
        let template = (DataTemplate)e.Value
        where dataType.IsAssignableFrom(type)
        select template;

    var template = entries.FirstOrDefault();
    if (template != null)
        return template;

    foreach(var mergedDic in resources.MergedDictionaries)
    {
        template = FindTemplateForType(dataType, mergedDic);
        if (template != null)
            return template;        
    }

    return null;
}

答案 2 :(得分:0)

我使用与karfus相同的方式,并将搜索datattemplate添加到baseType 如果有与Type.Basetype相关的datatemplate

<Extension>
Public Function GetDatatemplateForType(container As DependencyObject, dataType As Type) As DataTemplate
    Dim dTemplate As DataTemplate = Nothing
    Dim currentType As Type = dataType
    Do While dTemplate Is Nothing And currentType IsNot Nothing
        dTemplate = DataTemplateForType(container, currentType)
        currentType = currentType.BaseType
    Loop
    Return dTemplate
End Function

Private Function DataTemplateForType(Container As DependencyObject, dataType As Type) As DataTemplate
    Dim resTemplate As DataTemplate = Nothing
    Dim dKey As DataTemplateKey = New DataTemplateKey(dataType)
    Dim fm As FrameworkElement = TryCast(Container, FrameworkElement)
    If fm IsNot Nothing Then
        resTemplate = fm.TryFindResource(dKey)
        If resTemplate Is Nothing AndAlso fm.GetVisualParent(Of FrameworkElement) IsNot Nothing Then
            Return DataTemplateForType(fm.GetVisualParent(Of FrameworkElement), dataType)
        Else
            Return resTemplate
        End If
    End If
    Return Nothing

End Function