【问题标题】:Can I use subobjects of a StaticResource?我可以使用静态资源的子对象吗?
【发布时间】:2012-03-11 18:10:54
【问题描述】:

目前对项目之间的剪切和粘贴转换器感到有些无聊。

有什么方法可以使用具有转换器作为字段/属性的单个转换器对象?

例如类似:

<Application.Resources>
    <sharedLib:Converters
        x:Key="Converters" />
</Application.Resources>

<TextBlock Text="{Binding Target, Converter={StaticResource Converters.MakeAllCaps}}" />

如果没有,那么是否有人对如何批量导入转换器有任何建议?

【问题讨论】:

    标签: windows-phone-7 xaml staticresource converters


    【解决方案1】:

    您可以像这样在资源字典中定义所有转换器:

    <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}}" />
    

    【讨论】:

      猜你喜欢
      • 2013-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多