wpf data template is not working with contentcontr

2019-08-18 01:54发布

Just want to make mvvm dialog using answer from this question Using MVVM show new window and get updates data. I also use example from MVVM survival guide book. I have this ViewModel class for dialog:

public class OrganizationsViewModel
{

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

    public string TestProp { get; set; }
}

ShowDialog method in DialogService

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>

DataTemplate in Application.xaml

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

I call ShowDialog from MainWindowViewModel

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

And I can see only Organization string in dialog, but don't see OrganizationsView. Don't understand what is wrong.

标签: c# wpf xaml mvvm
0条回答
登录 后发表回答