【问题标题】:Accessing DataTemplate from ResourceDictionary in code behind在后面的代码中从 ResourceDictionary 访问 DataTemplate
【发布时间】:2017-08-28 12:53:43
【问题描述】:

我正在尝试将供应商提供的 VB 解决方案转换为 C#。我需要将自定义 ResourceDictionary XAML 中的 DataTemplate 加载到 c# 类中。我无法确定如何获取 DataTemplate。我能够创建一个 ResourceDictionary 并加载 XAML 但我从那里难住了。这是我的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>

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

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

【问题讨论】:

    标签: wpf datatemplate resourcedictionary


    【解决方案1】:

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

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

    有关更完整的示例,另请参阅following article

    【讨论】:

    • 谢谢。这正是我想要做的。
    【解决方案2】:

    WPF 中的框架元素包含一个FindResource 方法,该方法通过键在应用程序范围内搜索资源。

    看看documentation。您可以通过 Key 获取 DataTemplate,然后在您的代码隐藏文件中访问它。

    在这种情况下,这对您有帮助吗?如果不是,请说明您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 2012-04-10
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      相关资源
      最近更新 更多