wpfxamlstaticresource

How to set a dependency property of a static resource?


I would like to add a reference to my model in my view model using XAML. I have defined my model and my view model as resources, and I would like to bind the dependency property Model of the resource myViewModel to the resource myModel.

How do I do this using XAML?

Below is my attempt:

<Window.Resources>
    <local:ViewModel x:Key="myViewModel" />
    <local:Model x:Key="myModel" />
</Window.Resources>

<myViewModel Model="{StaticResource myModel}">

Solution

  • This works fine:

    <Window.Resources>
        <local:Model x:Key="myModel" />
        <local:ViewModel x:Key="myViewModel" Model="{StaticResource myModel}" />
    </Window.Resources>
    

    Assuming, of course, the model data structures are declared appropriately (you didn't provide that context).