WPF:与ContentControl一起使用时,DataTemplate中的数据未加载

时间:2015-06-26 14:46:21

标签: c# wpf mvvm datatemplate

我正在使用ContentControl和DataTemplate来加载数据但数据没有被加载。

<DataTemplate DataType="{x:Type data:VesselInspectionSummaryViewModel}">                     
<StackPanel>                                  
<ContentControl Content="{Binding InternalInspections}" ContentTemplate="{StaticResource  InternalInspectionSummaryDataTemplate}"  ></ContentControl>
</StackPanel>....

我对DataTemplate内部的ContentControl的关注,InternalInspectionSummaryDataTemplate的定义如下所述。

<DataTemplate x:Key="InternalInspectionSummaryDataTemplate" >
        <TextBlock Text="{Binding  Value}"   Style="{StaticResource   HomeDetailsTitleFontStyle}"  />
</DataTemplate>

但不知怎的,我无法显示TextBlock的文本,这是一个“值”。 任何人都可以帮助我获得字段textBlock的价值。

1 个答案:

答案 0 :(得分:2)

Finall我遇到了这个问题,它与xaml无关,但在Viewmodel中。 以前的代码是。

private readonly ObservableCollection<InspectionUrgencyDetailViewModel> _externalInspections;
public ObservableCollection<InspectionUrgencyDetailViewModel> ExternalInspections { get; set; }

private readonly ObservableCollection<InspectionUrgencyDetailViewModel> _internalInspections;
public ObservableCollection<InspectionUrgencyDetailViewModel> InternalInspections { get; set; }

如果您看到属性与私有变量不对应,请将其更改为以下。

        private ObservableCollection<InspectionUrgencyDetailViewModel> _externalInspections;
        public ObservableCollection<InspectionUrgencyDetailViewModel> ExternalInspections
        {
            get { return _externalInspections; }
            set { Set(() => ExternalInspections, ref _externalInspections, value); }
        }

        private ObservableCollection<InspectionUrgencyDetailViewModel> _internalInspections;
        public ObservableCollection<InspectionUrgencyDetailViewModel> InternalInspections
        {
            get { return _internalInspections; }
            set { Set(() => InternalInspections, ref _internalInspections, value); }