开发者

Mvvm and Mef and Item control template

开发者 https://www.devze.com 2023-04-06 10:45 出处:网络
I have the following case on my WPF app : I have a main user control with its MainViewModel which binded to the control datacontext .

I have the following case on my WPF app :

I have a main user control with its MainViewModel which binded to the control datacontext .

Inside the main control , I have an item control which item source is binded to List .

the item control has a user control as its template which draw the users details , each template as we know has User as its Datacontext .

My question :

How I can use the Main control datacontext (MainViewModel) in the template user control . I tried to use mef to import MainViewModel but the problem its exported with creation policy开发者_如何学JAVA "NonShared" so the import will bring new instances of the MainViewModel and not the one of the Main control .

Thanks in advance ...


If you are trying to use MEF, you could always compose the existing value with a unique name, e.g.:

MainViewModel vm = DataContext;
container.ComposeExportedValue("SharedViewModel", vm);

That way, you can always provide an import for it in your user control:

[Import("SharedViewModel")]
public MainViewModel ViewModel { get; set; }

Where ComposeExportedValue is an extension method belonging to the AttributedModelServices type.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号