Ben Morris
Ben Morris

Reputation: 81

Binding to XAML resource

I am creating a page-based WPF application using MVVM. I have created a custom (non dependency object) helper class to centralize navigation. This class is created as a resource of my main window like so.

<Window.Resources>
    <local:NavigationManager x:Key="NavigationManagerKey" x:Name="NavigationManager"/>
</Window.Resources>

The class contains an ICommand that I have exposed publicly so that it can be used in XAML. However, I am struggling to find out how to bind to it. I would prefer not to have to set it as the data context for the page as that is already in use. Normally, I bind to a command like so (when I am binding to a command on the data context)

<Button Header="Image" Command="{Binding CreateImageAssetCommand}"></Button>

Thanks for any help with the matter.

Upvotes: 0

Views: 509

Answers (1)

Bas
Bas

Reputation: 27115

You can set the source of the binding:

<Button Header="Image" Command="{Binding CreateImageAssetCommand, Source={StaticResource NavigationManagerKey}}"></Button>

Upvotes: 1

Related Questions