DataContext未填充在本地用户控件

时间:2017-03-25 03:41:04

标签: c# wpf xaml datacontext

我有以下文件,App.xamlDataTemplates.xamlExcelReport.xamlMainWindow.xaml及其代码。cs文件,所有文件都在同一个文件夹中。

以下是文件的内容:

DataTemplates.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   ...
                    xmlns:local="clr-namespace:WpfApplication1">

    <DataTemplate DataType="{x:Type local:ExcelReportVM}">
        <local:ExcelReport DoubleClickHandler="{Binding}">
          </local:ExcelReport>
    </DataTemplate>
</ResourceDictionary>

的App.xaml

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            ....
             xmlns:local="clr-namespace:WpfApplication1"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="DataTemplates.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

ExcelReport.xaml

<UserControl x:Class="WpfApplication1.ExcelReport"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            ....
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
             xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <DataTemplate x:Key="HeaderTemplate">
            <TextBlock Text="{Binding .}"  />
        </DataTemplate>
    </UserControl.Resources>
    <syncfusion:SfDataGrid ItemsSource="{Binding Entries}" x:Name="grid">
    </syncfusion:SfDataGrid>
</UserControl>

ExcelReport.xaml.cs

 public partial class ExcelReport : UserControl
{
        public ExcelReport()
        {
            InitializeComponent();
            this.Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            if(DataContext==null)
            {
                   throw new Exception("DataContext shouldn't be null");
            }
        }

}

MainWindow.xaml

<Syncfusion:RibbonWindow x:Class="WpfApplication1.MainWindow"
        ....
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" WindowState="Maximized"
        Syncfusion:SkinStorage.VisualStyle="Office2013"
        xmlns:Syncfusion="http://schemas.syncfusion.com/wpf">

    <Grid x:Name="ExcelReport">
        <local:ExcelReport />
    </Grid>
</Syncfusion:RibbonWindow>

但是,当我启动应用程序时,我发现ExcelReport的DataContext始终为空,即使在onLoaded方法中也是如此。我希望由于我已在DataTemplate中声明DataTemplates.xaml,因此ExcelReport DataContext将为ExcelReportVM。为什么不是这样的?

1 个答案:

答案 0 :(得分:3)

DataTemplate仅指定WPF如何显示特定对象。它实际上并不创建对象(在本例中为VM)。

您仍需要将视图中的DataContext设置为ExcelReportVM类型的新对象。

public ExcelReport()
{
    InitializeComponent();
    DataContext = new ExcelReportVM();

    this.Loaded += OnLoaded;
}

请参阅此StackOverflow问题以获得更深入的答案:https://stackoverflow.com/questions/2407917/what-is-the-difference-between-datatemplate-and-datacontext-in-wpf