简体   繁体   English

绑定为静态资源

[英]Binding as staticresource

I had the following code: 我有以下代码:

    <!-- 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>

And in an effort to reduce some of the copy and paste from the bindings, I tried creating a common binding as a static resource in the resource dictionary: 为了减少绑定中的某些复制和粘贴,我尝试在资源字典中创建一个公共绑定作为静态资源:

    <!-- 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>

But my program threw up the following exception: A 'Binding' cannot be used within a 'ResourceDictionary' collection. 但是我的程序抛出以下异常: 'ResourceDictionary'集合中不能使用'Binding'。 A 'Binding' can only be set on a DependencyProperty of a DependencyObject. 只能在DependencyObject的DependencyProperty上设置“绑定”。

Is there a WPF/MVVM accepted way of reducing this kind of bloat (and the chance of an error arising due to copy/paste)? 是否有WPF / MVVM可接受的方法来减少这种膨胀(以及由于复制/粘贴而导致错误的可能性)?

I think that the best option is to rewrite your IconTrigger class. 我认为最好的选择是重写IconTrigger类。 It should store collection of values and canvas, so you can write something like this: 它应该存储值和画布的集合,因此您可以编写如下内容:

    <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM