在后面的代码中从ResourceDictionary访问DataTemplate [英] Accessing DataTemplate from ResourceDictionary in code behind

查看:87
本文介绍了在后面的代码中从ResourceDictionary访问DataTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将供应商提供的VB解决方案转换为 C#。我需要将数据模板从自定义ResourceDictionary XAML 加载到 c#类中。我无法确定如何获取DataTemplate。我能够创建ResourceDictionary并加载 XAML ,但是从那里被困住了。这是我的 XAML [EditorResources]。

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 .

<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>

这是我需要转换的VB代码:

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


推荐答案

据我了解, res 变量是从 ResourceDictionary 派生的类的实例。在这种情况下,您可以非常容易地获得数据模板:

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.

这篇关于在后面的代码中从ResourceDictionary访问DataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆