amiry jd
amiry jd

Reputation: 27585

Accessing a UserControl's resource from itself

Is it possible to access a defined resource in a UserControl from itself? How?

I'm trying to do this:

<UserControl xmlns="all defined namespaces here..."
             Visibility="{Binding Show,FallbackValue=Hidden,
                 Converter={StaticResource BooleanToVisibility}}">
    <UserControl.Resources>
        <ResourceDictionary>
            <BooleanToVisibilityConverter x:Key="BooleanToVisibility" />
        </ResourceDictionary>
    </UserControl.Resources>
</UserControl>

, but I get this error:

'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '11' and line position '9'.

Upvotes: 1

Views: 269

Answers (1)

brunnerh
brunnerh

Reputation: 185057

Can probably change the order:

<UserControl.Resources>
    <ResourceDictionary>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibility" />
    </ResourceDictionary>
</UserControl.Resources>
<UserControl.Visibility>
    <Binding Path="Show" FallbackValue="Hidden"
             Converter="{StaticResource BooleanToVisibility}"/>
</UserControl.Visibility>

You could also move the converter to the Application.Resources as it's commonly used in multiple places, then it can be referenced anywhere.

Upvotes: 3

Related Questions