DataContext 未填充在本地用户控件内

DataContext is not populated inside a local user control

我有以下文件,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 方法中也是如此。我希望因为我已经在 DataTemplates.xaml 中声明了 DataTemplate,所以 ExcelReport DataContext 将是 ExcelReportVM。为什么不是这样呢?

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

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

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

    this.Loaded += OnLoaded;
}

请参阅此 Whosebug 问题以获得更深入的答案: