Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML trigger as StaticResource

Tags:

wpf

xaml

triggers

Why can't I create a trigger and use it as a static resource in XAML?

<Window.Resources>
    <Trigger x:Key="ValidationTrigger" x:Shared="False" Property="Validation.HasError" Value="true">
        <Setter Property="FrameworkElement.ToolTip"
                Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                Path=(Validation.Errors)/ErrorContent}"/>
    </Trigger>

    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
        <Style.Triggers>
            <StaticResource ResourceKey="ValidationTrigger"/>
        </Style.Triggers>
    </Style>
</Window.Resources>

I get an errormessage at runtime
"Value cannot be null.
Parameter name: triggerBase Error at object 'System.Windows.Markup.StaticResourceHolder' in markup file"

like image 365
adrianm Avatar asked Apr 30 '10 09:04

adrianm


1 Answers

I imagine that this is because resources are loaded first.

So in this case, WPF would try to load your trigger before any control it might be associated with. Then the trigger value can't be evaluated at that time.

But that need to be confirmed :)

like image 125
Bruno Avatar answered Oct 27 '22 17:10

Bruno