我在項目中兩次使用了列表框樣式,所以我想在資源字典中使用它。這個列表框樣式有兩個值轉換器,所以我在相同的資源文件中實例化了轉換器。儘管它在運行時聲明「未知類型不能聲明」,儘管在mainwindow.xaml文件中使用它們時工作原理相同。valueconverter在資源字典中的未知
任何人有想法?
我在項目中兩次使用了列表框樣式,所以我想在資源字典中使用它。這個列表框樣式有兩個值轉換器,所以我在相同的資源文件中實例化了轉換器。儘管它在運行時聲明「未知類型不能聲明」,儘管在mainwindow.xaml文件中使用它們時工作原理相同。valueconverter在資源字典中的未知
任何人有想法?
我有相同的問題,直到我將轉換器移動到資源部分下的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
你可以發佈一些代碼(listboxstyle與轉換器的聲明)我看不出有什麼問題,這樣做 – blindmeis 2012-01-02 12:15:07