2017-08-28 87 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,然後在代碼隱藏文件中訪問它。

這是什麼幫助你在這種情況下?如果沒有,請說明你的問題。

相關問題