使用来自dll的ViewModel进行UserControl

时间:2016-04-08 15:13:47

标签: c# .net wpf mvvm

我有一个包含usercontrol的dll以及控件的viewmodel。我在另一个应用程序中使用此usercontrol。现在我不知道如何将datacontext设置为dll中的viewmodel。

这是我的应用程序的视图代码(Window LayoutControlViewModel是dll ViewModel):

<Window x:Class="TestApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dll="clr-namespace:WindowLayoutControl.View;assembly=WindowLayoutControl"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <dll:WindowLayoutControl DataContext="{Binding WindowLayoutControlViewModel}"></dll:WindowLayoutControl>
</Grid>

对不起,我对mvvm和wpf很新。提前谢谢。

1 个答案:

答案 0 :(得分:2)

你说在那个dll中你同时拥有View和ViewModel,所以我假设ViewModel在一个单独的命名空间中:

<Window x:Class="TestApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dll="clr-namespace:WindowLayoutControl.View;assembly=WindowLayoutControl"
    xmlns:dllViewModel="clr-namespace:WindowLayoutControl.ViewModel;assembly=WindowLayoutControl"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <dll:WindowLayoutControl>
        <dll:WindowLayoutControl.DataContext>
            <dllViewModel:{Name of your view model here}/>
        </dll:WindowLayoutControl.DataContext>
    </dll:WindowLayoutControl>
</Grid>