您可以像这样在资源字典中定义所有转换器:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="...">
<loc:BooleanToVisibilityConverter x:Key="BooleanToVisibility" />
<loc:MakeAllCapsConverter x:Key="MakeAllCaps" />
<!-- Define all the common converters here -->
</ResourceDictionary>
现在您可以通过MergedDictionaries
以下方式在任何地方导入此资源字典:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Converters.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
<TextBlock Text="{Binding Target, Converter={StaticResource MakeAllCaps}}" />