wpf数据模板在mvvm对话框中不能与contentcontrol一起使用

时间:2014-12-04 13:18:29

标签: c# wpf xaml mvvm

只想使用此问题Using MVVM show new window and get updates data的答案进行mvvm对话。我还使用了MVVM生存指南中的示例。 我有这个用于对话框的ViewModel类:

public class OrganizationsViewModel
{

    public OrganizationsViewModel()
    {
        TestProp = "TEST prop";
    }
    public override string ToString()
    {
        return "Organization";
    }

    public string TestProp { get; set; }
}

DialogService中的ShowDialog方法

public void ShowDialog(OrganizationsViewModel viewModel)
{
    var dialog = new DialogView() { DataContext = viewModel };
    dialog.Owner = Application.Current.MainWindow;
    dialog.ShowInTaskbar = false;
    dialog.ShowDialog();
}

DialogView.xaml:

<Window x:Class="testlayout.DialogView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DialogView" Height="300" Width="300">
    <StackPanel>
        <ContentControl Content="{Binding}" />      
    </StackPanel>
</Window>

Application.xaml中的DataTemplate

<Application.Resources>        
    <DataTemplate x:Key="OrganizationsTemplate" DataType="{x:Type vm:OrganizationsViewModel}">
        <vw:OrganizationsView/>
    </DataTemplate>   
</Application.Resources>

我从MainWindowViewModel调用ShowDialog

  DialogService.Instance.ShowDialog(new OrganizationsViewModel());

我只能在对话框中看到组织字符串,但看不到OrganizationsView。不明白出了什么问题。

0 个答案:

没有答案