首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绑定为静态资源

绑定为静态资源
EN

Stack Overflow用户
提问于 2016-02-29 17:16:51
回答 1查看 401关注 0票数 0

我有以下代码:

代码语言:javascript
复制
    <!-- Automatically apply icons to context menus based on the header value -->
<Style x:Key="DecoratedMenuItem" TargetType="MenuItem" BasedOn="{StaticResource MenuItem}">
    <Style.Resources>
        <clr:Double x:Key="CanvasSize">16</clr:Double>
        <converters:IgnoreUnderscoresConverter x:Key="IgnoreUnderscoresConverter" />
    </Style.Resources>
    <Style.Triggers>
        <local:IconTrigger Binding="{Binding Header, RelativeSource={RelativeSource Self}, Converter={StaticResource IgnoreUnderscoresConverter}}" IconSize="{StaticResource CanvasSize}" Value="Run" Canvas="{StaticResource appbar_control_play}" />
        <local:IconTrigger Binding="{Binding Header, RelativeSource={RelativeSource Self}, Converter={StaticResource IgnoreUnderscoresConverter}}" IconSize="{StaticResource CanvasSize}" Value="Edit" Canvas="{StaticResource appbar_edit}" />
        <local:IconTrigger Binding="{Binding Header, RelativeSource={RelativeSource Self}, Converter={StaticResource IgnoreUnderscoresConverter}}" IconSize="{StaticResource CanvasSize}" Value="Delete" Canvas="{StaticResource appbar_delete}" />
        <local:IconTrigger Binding="{Binding Header, RelativeSource={RelativeSource Self}, Converter={StaticResource IgnoreUnderscoresConverter}}" IconSize="{StaticResource CanvasSize}" Value="Install" Canvas="{StaticResource appbar_social_dropbox_download}" />
        <local:IconTrigger Binding="{Binding Header, RelativeSource={RelativeSource Self}, Converter={StaticResource IgnoreUnderscoresConverter}}" IconSize="{StaticResource CanvasSize}" Value="Uninstall" Canvas="{StaticResource appbar_delete}" />
        <local:IconTrigger Binding="{Binding Header, RelativeSource={RelativeSource Self}, Converter={StaticResource IgnoreUnderscoresConverter}}" IconSize="{StaticResource CanvasSize}" Value="Advanced" Canvas="{StaticResource appbar_tardis}" />
        <local:IconTrigger Binding="{Binding Header, RelativeSource={RelativeSource Self}, Converter={StaticResource IgnoreUnderscoresConverter}}" IconSize="{StaticResource CanvasSize}" Value="Undo" Canvas="{StaticResource appbar_undo}" />
        <local:IconTrigger Binding="{Binding Header, RelativeSource={RelativeSource Self}, Converter={StaticResource IgnoreUnderscoresConverter}}" IconSize="{StaticResource CanvasSize}" Value="Redo" Canvas="{StaticResource appbar_redo}" />
        <local:IconTrigger Binding="{Binding Header, RelativeSource={RelativeSource Self}, Converter={StaticResource IgnoreUnderscoresConverter}}" IconSize="{StaticResource CanvasSize}" Value="Exit" Canvas="{StaticResource appbar_close}" />
        <local:IconTrigger Binding="{Binding Header, RelativeSource={RelativeSource Self}, Converter={StaticResource IgnoreUnderscoresConverter}}" IconSize="{StaticResource CanvasSize}" Value="Close" Canvas="{StaticResource appbar_close}" />
        <local:IconTrigger Binding="{Binding Header, RelativeSource={RelativeSource Self}, Converter={StaticResource IgnoreUnderscoresConverter}}" IconSize="{StaticResource CanvasSize}" Value="Copy" Canvas="{StaticResource appbar_page_copy}" />
        <local:IconTrigger Binding="{Binding Header, RelativeSource={RelativeSource Self}, Converter={StaticResource IgnoreUnderscoresConverter}}" IconSize="{StaticResource CanvasSize}" Value="Cut" Canvas="{StaticResource appbar_scissor}" />
        <local:IconTrigger Binding="{Binding Header, RelativeSource={RelativeSource Self}, Converter={StaticResource IgnoreUnderscoresConverter}}" IconSize="{StaticResource CanvasSize}" Value="Paste" Canvas="{StaticResource appbar_clipboard_paste}" />
    </Style.Triggers>
</Style>

为了减少绑定中的一些复制和粘贴,我尝试在资源字典中创建一个公共绑定,作为静态资源:

代码语言:javascript
复制
    <!-- Automatically apply icons to context menus based on the header value -->
<Style x:Key="DecoratedMenuItem" TargetType="MenuItem" BasedOn="{StaticResource MenuItem}">
    <Style.Resources>
        <clr:Double x:Key="CanvasSize">16</clr:Double>
        <converters:IgnoreUnderscoresConverter x:Key="IgnoreUnderscoresConverter" />
        <Binding x:Key="HeaderBinding"  Path="Header" RelativeSource="{RelativeSource Self}" Converter="{StaticResource IgnoreUnderscoresConverter}" />
    </Style.Resources>
    <Style.Triggers>
        <local:IconTrigger Binding="{StaticResource HeaderBinding}" IconSize="{StaticResource CanvasSize}" Value="Run" Canvas="{StaticResource appbar_control_play}" />
        <local:IconTrigger Binding="{StaticResource HeaderBinding}" IconSize="{StaticResource CanvasSize}" Value="Edit" Canvas="{StaticResource appbar_edit}" />
        <local:IconTrigger Binding="{StaticResource HeaderBinding}" IconSize="{StaticResource CanvasSize}" Value="Delete" Canvas="{StaticResource appbar_delete}" />
        <local:IconTrigger Binding="{StaticResource HeaderBinding}" IconSize="{StaticResource CanvasSize}" Value="Install" Canvas="{StaticResource appbar_social_dropbox_download}" />
        <local:IconTrigger Binding="{StaticResource HeaderBinding}" IconSize="{StaticResource CanvasSize}" Value="Uninstall" Canvas="{StaticResource appbar_delete}" />
        <local:IconTrigger Binding="{StaticResource HeaderBinding}" IconSize="{StaticResource CanvasSize}" Value="Advanced" Canvas="{StaticResource appbar_tardis}" />
        <local:IconTrigger Binding="{StaticResource HeaderBinding}" IconSize="{StaticResource CanvasSize}" Value="Undo" Canvas="{StaticResource appbar_undo}" />
        <local:IconTrigger Binding="{StaticResource HeaderBinding}" IconSize="{StaticResource CanvasSize}" Value="Redo" Canvas="{StaticResource appbar_redo}" />
        <local:IconTrigger Binding="{StaticResource HeaderBinding}" IconSize="{StaticResource CanvasSize}" Value="Exit" Canvas="{StaticResource appbar_close}" />
        <local:IconTrigger Binding="{StaticResource HeaderBinding}" IconSize="{StaticResource CanvasSize}" Value="Close" Canvas="{StaticResource appbar_close}" />
        <local:IconTrigger Binding="{StaticResource HeaderBinding}" IconSize="{StaticResource CanvasSize}" Value="Copy" Canvas="{StaticResource appbar_page_copy}" />
        <local:IconTrigger Binding="{StaticResource HeaderBinding}" IconSize="{StaticResource CanvasSize}" Value="Cut" Canvas="{StaticResource appbar_scissor}" />
        <local:IconTrigger Binding="{StaticResource HeaderBinding}" IconSize="{StaticResource CanvasSize}" Value="Paste" Canvas="{StaticResource appbar_clipboard_paste}" />
    </Style.Triggers>
</Style>

但是我的程序抛出了以下异常:A‘绑定’不能在'ResourceDictionary‘集合中使用。“绑定”只能在DependencyProperty of a DependencyObject.上设置。

是否有一种WPF/MVVM接受的方法来减少这种膨胀(以及由于复制/粘贴而产生错误的可能性)?

EN

回答 1

Stack Overflow用户

发布于 2016-02-29 17:36:26

我认为最好的选择是重写您的IconTrigger类。它应该存储值和画布的集合,这样您就可以编写如下内容:

代码语言:javascript
复制
    <local:IconTrigger Binding="{Binding Header, RelativeSource={RelativeSource Self}, Converter={StaticResource IgnoreUnderscoresConverter}}"  IconSize="{StaticResource CanvasSize}"> 
        <local:IconTriggerValue Value="Run" Canvas="{StaticResource appbar_control_play}" />
        <local:IconTriggerValue Value="Edit" Canvas="{StaticResource appbar_edit}" />
        <local:IconTriggerValue Value="Delete" Canvas="{StaticResource appbar_delete}" />
        <local:IconTriggerValue Value="Install" Canvas="{StaticResource appbar_social_dropbox_download}" />
        <local:IconTriggerValue Value="Uninstall" Canvas="{StaticResource appbar_delete}" />
        <local:IconTriggerValue Value="Advanced" Canvas="{StaticResource appbar_tardis}" />
        <local:IconTriggerValue Value="Undo" Canvas="{StaticResource appbar_undo}" />
        <local:IconTriggerValue Value="Redo" Canvas="{StaticResource appbar_redo}" />
        <local:IconTriggerValue Value="Exit" Canvas="{StaticResource appbar_close}" />
        <local:IconTriggerValue Value="Close" Canvas="{StaticResource appbar_close}" />
        <local:IconTriggerValue Value="Copy" Canvas="{StaticResource appbar_page_copy}" />
        <local:IconTriggerValue Value="Cut" Canvas="{StaticResource appbar_scissor}" />
        <local:IconTriggerValue Value="Paste" Canvas="{StaticResource appbar_clipboard_paste}" />
    </local:IconTrigger>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35705490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档