4

I have a window in which I want to display dynamic content based on a datatemplate.

The XAML looks roughly like this:

<Window x:Class="Report.ControlLibrary.ReportHost"
         xmlns:reports="clr-namespace:Report.ControlLibrary.Reports"
\>
<Window.Resources>
    <DataTemplate x:Key="ReportTemplate" DataType="{x:Type reports:ReportViewModel}">
        <!-- Markup for DataTemplate goes here -->         
    </DataTemplate>
</Window.Resources>   
<ContentControl Content="{Binding}">

</ContentControl>
</Window>

The DataContext is being set by:

var vm = new ReportViewModel();
var report = new ReportHost() { DataContext=vm };          
report.Show();

It seems like the DataContext on the Window is set correctly but the DataTemplate is not found as the window just contains the ToString representation of my ViewModel.

4

2 回答 2

6

您是否有任何机会在数据模板中定义了 x:Key。如果是这样,那将导致内容演示者不使用数据模板。这是我最近遇到的一个问题。

于 2013-05-21T12:59:04.423 回答
-1

尝试使用 ContentControl.ContentTemplate 设置要用于内容的 DataTemplate。看看这里的解释

于 2013-05-21T01:07:29.643 回答