2017-08-28 83 views
0

我试图将供应商提供的VB解决方案转换为C#。我需要从一个自定义的ResourceDictionary XAML加载一个DataTemplate到一个c#类。我无法确定如何获取DataTemplate。我能够创建一个ResourceDictionary并加载XAML但我从那里难倒了。这是我的XAML [EditorResources]。从代码背后的ResourceDictionary中访问DataTemplate

<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 

回答

0

据我所知,res变量是从ResourceDictionary派生的类的实例。在这种情况下,你可以得到数据模板非常简单:

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

following article见的更完整的例子。

+0

谢谢。这正是我想要做的。 – Schrecengost

+0

很高兴为您服务!请将答案标记为已接受。 – Pavel

0

骨架元素在WPF包含FindResource方法,其搜索的关键资源的应用范围。

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

这是什么帮助你在这种情况下?如果没有,请说明你的问题。

相关问题