2016-02-29 3 views
0

я имел следующий код:Связывание, как StaticResource

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

И в попытке сократить некоторые операции копирования и вставки из переплетов, я попытался создать общее связывание как статический ресурс в словаре ресурсов:

<!-- 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 'Binding' не может быть использована в фондах 'ResourceDictionary'. «Связывание» может быть установлено только на DependencyProperty объекта DependencyObject.

Есть ли одобренный WPF/MVVM способ уменьшения такого раздувания (и вероятность возникновения ошибки из-за копирования/вставки)?

ответ

0

Я думаю, что лучший вариант - переписать свой класс IconTrigger. Он должен хранить коллекцию ценностей и холста, поэтому вы можете написать примерно так:

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