Accessing DataTemplate from ResourceDictionary in

2019-08-19 03:11发布

问题:

I am trying to convert a vendor provided VB solution to C#. I need to load a DataTemplate from a custom ResourceDictionary XAML into a c# class. I cannot determine how to get the DataTemplate. I am able to create a ResourceDictionary and load the XAML but am stumped from there. Here is my XAML [EditorResources].

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:PropertyEditing="clr-namespace:Microsoft.Windows.Design.PropertyEditing;assembly=Microsoft.Windows.Design.Interaction"
                    xmlns:Local="clr-namespace:MyControls.Design"
                    xmlns:my="clr-namespace:MyControls;assembly=MyControls"
                    x:Class="EditorResources">
    <DataTemplate x:Key="TagBrowserInlineEditorTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBox Grid.Column="0" Text="{Binding StringValue}"/>
            <PropertyEditing:EditModeSwitchButton Grid.Column="1"/>
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="template">
        <Border BorderThickness="2" 
                BorderBrush="Black">
            <TextBlock Text="{Binding Path=value}" Padding="2" />
        </Border>
    </DataTemplate>

</ResourceDictionary>

Here is the VB code that I need to convert:

Imports System
Imports System.ComponentModel
Imports System.Windows
Imports Microsoft.Windows.Design.Metadata
Imports Microsoft.Windows.Design.PropertyEditing
Imports Microsoft.Win32

Public Class TagBrowserDialogPropertyValueEditor
    Inherits DialogPropertyValueEditor
    Private res As New EditorResources()

    Public Sub New()
        Me.InlineEditorTemplate = TryCast(res("TagBrowserInlineEditorTemplate"), DataTemplate)
    End Sub

    Public Overloads Overrides Sub ShowDialog(ByVal propertyValue As PropertyValue, ByVal commandSource As IInputElement)
        Dim frmBrowseTagParameter As New OPCWPFDashboard.Design.FormBrowseTagParameter
        If frmBrowseTagParameter Is Nothing Then
            frmBrowseTagParameter = New OPCWPFDashboard.Design.FormBrowseTagParameter
        End If

        If frmBrowseTagParameter.ShowDialog = Forms.DialogResult.OK Then
            propertyValue.StringValue = frmBrowseTagParameter.Final_Tag
        End If

    End Sub


End Class

回答1:

As far as I understand, res variable is the instance of the class derived from ResourceDictionary. In this case you can get the data template very easy:

this.InlineEditorTemplate = res["TagBrowserInlineEditorTemplate"] as DataTemplate;

See also the following article for the more complete example.



回答2:

A Framework Element in WPF contains a FindResource method, which searches the application scope for a resource by key.

Have a look at the documentation. You can get the DataTemplate by Key and then access it in your code behind file.

Is this what will help you in this case? If not, please specify your question.