简体   繁体   中英

Set DataContext to a property of a StaticResource

I have a Controller class which manages top components:

public class FooViewModel {}

public class Controller
{
    public FooViewModel Foo1ViewModel {get; protected set;} // = new ...;
}

I have a static resource of the Controller :

<Application.Resources>
    <local:Controller x:Key="AppController" />
</Application.Resources>

And I want Windows and UserControls to have as DataContext properties of Controller .

As far as I got is to set the DataContext to the Controller itself (which is not what I want)

<Window.DataContext>
    <StaticResource ResourceKey="AppController" />
</Window.DataContext>

but I cannot set it to a property of it

<Window.DataContext>
    <!-- something like this path: -->
    <!--  AppController.Foo1ViewModel -->
</Window.DataContext>

Use a Binding with the Controller instance as Source:

<Window ...
    DataContext="{Binding Foo1ViewModel, Source={StaticResource AppController}}">

Use a Binding :

<Window.DataContext>
    <Binding Path="Foo1ViewModel" Source="{StaticResource AppController}" />
</Window.DataContext>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM