2012-01-02 36 views
0

我在項目中兩次使用了列表框樣式,所以我想在資源字典中使用它。這個列表框樣式有兩個值轉換器,所以我在相同的資源文件中實例化了轉換器。儘管它在運行時聲明「未知類型不能聲明」,儘管在mainwindow.xaml文件中使用它們時工作原理相同。valueconverter在資源字典中的未知

任何人有想法?

+1

你可以發佈一些代碼(listboxstyle與轉換器的聲明)我看不出有什麼問題,這樣做 – blindmeis 2012-01-02 12:15:07

回答

4

我有相同的問題,直到我將轉換器移動到資源部分下的App.xaml文件。這是我的示例App.xaml文件,我剛創建了一個名爲TextConverter的轉換器作爲示例。

有兩種方法,如果你使用的是你必須使用在所有情況下ResourceDictionary.MergedDictionaries其他ResourceDictionaries要做到這一點,如下所示:

<Application x:Class="WPFFeatureSample_Application.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:converters="clr-namespace:WPFFeatureSample_Application.Converters" 
     StartupUri="MainWindow.xaml"> 
<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary> 
       <converters:TextConverter x:Key="TextConverter1"></converters:TextConverter> 
      </ResourceDictionary> 
      <ResourceDictionary Source="Resources/ControlDictionary.xaml"></ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

如果沒有其他資源檔案,這將是這樣的:

<Application x:Class="WPFFeatureSample_Application.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:converters="clr-namespace:WPFFeatureSample_Application.Converters" 
     StartupUri="MainWindow.xaml"> 
<Application.Resources> 
    <ResourceDictionary> 
     <converters:TextConverter x:Key="TextConverter1"></converters:TextConverter> 
    </ResourceDictionary> 
</Application.Resources> 

一個有趣的參考使用一般的轉換器和資源字典的時候是這樣的: http://www.dotnetdude.com/2011/01/23/ResourceDictionariesAreMakingMyHairHurt.aspx

+0

這幫助我,謝謝! – raphi011 2012-01-09 10:37:05

+0

這對我也有幫助。它不需要在App.xaml字典中。關鍵是將轉換器封裝在標籤中,並將其放在Resources或MergedDictionaries中。 – Eternal21 2014-06-20 18:33:21