wpf - 为什么在 ContentPresenter 上使用样式时会出现 NullReferenceException?

标签 wpf styles datatemplate contentpresenter

我创建了这个模板,它使用应用于 ContentPresenter 的样式,这样我就可以将数据对象的 Column 属性绑定(bind)到 Grid.Column,允许项目自行确定它们进入 Grid 的哪一列:

<DataTemplate DataType="{x:Type local:MyObject}">
  <ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.Resources>
      <Style TargetType="{x:Type ContentPresenter}">
        <Setter Property="Grid.Column"
                Value="{Binding Column}" />
      </Style>
    </ItemsControl.Resources>
    <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
          </Grid.ColumnDefinitions>
        </Grid>
      </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
  </ItemsControl>
</DataTemplate>

当我运行该程序时,我得到一个 NullReferenceException。长得可笑的堆栈跟踪的开头:

at System.Windows.StyleHelper.ApplyAutoAliasRules(OptimizedTemplateContent optimizedTemplateContent, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate, FrugalStructList`1& childRecordFromChildIndex, FrugalStructList`1& triggerSourceRecordFromChildIndex, FrugalStructList`1& resourceDependents, HybridDictionary& dataTriggerRecordFromBinding, Boolean& hasInstanceValues)
at System.Windows.StyleHelper.ProcessTemplateContent(FrameworkTemplate frameworkTemplate, FrugalStructList`1& childRecordFromChildIndex, FrugalStructList`1& triggerSourceRecordFromChildIndex, FrugalStructList`1& resourceDependents, ItemStructList`1& eventDependents, HybridDictionary& dataTriggerRecordFromBinding, HybridDictionary childIndexFromChildID, Boolean& hasInstanceValues)
at System.Windows.StyleHelper.SealTemplate(FrameworkTemplate frameworkTemplate, Boolean& isSealed, FrameworkElementFactory templateRoot, TriggerCollection triggers, ResourceDictionary resources, HybridDictionary childIndexFromChildID, FrugalStructList`1& childRecordFromChildIndex, FrugalStructList`1& triggerSourceRecordFromChildIndex, FrugalStructList`1& containerDependents, FrugalStructList`1& resourceDependents, ItemStructList`1& eventDependents, HybridDictionary& triggerActions, HybridDictionary& dataTriggerRecordFromBinding, Boolean& hasInstanceValues, EventHandlersStore& eventHandlersStore)
at System.Windows.FrameworkTemplate.Seal()
at System.Windows.StyleHelper.UpdateTemplateCache(FrameworkElement fe, FrameworkTemplate oldTemplate, FrameworkTemplate newTemplate, DependencyProperty templateProperty)
at System.Windows.Controls.ContentPresenter.OnTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)

...等等

这不是绑定(bind)。例如,如果我将样式 setter 中的值明确设置为 0,我仍然会收到错误消息。如果我删除样式,错误就会消失,尽管所有项目最终都会出现在第 0 列中。

这是怎么回事?我该如何调试这样的问题?

最佳答案

事实证明,解决这个问题很简单:将样式从 ItemsControl 的资源字典移动到 DataTemplate 的。

不过,我仍然不知道为什么会出现空引用异常。

关于wpf - 为什么在 ContentPresenter 上使用样式时会出现 NullReferenceException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2550701/

相关文章:

c# - WPF:ValueConverter (IValueConverter) 不起作用

Silverlight:按名称获取 DataTemplate 中的元素

WPF - 阻止列表框项目选择

c# - EntityObjects 是否有允许我访问 OldValue 和 NewValue 的 PropertyChanged 事件?

wpf - 适用于业务线应用程序的示例 WPF 应用程序?

javascript - 向下滚动页面时如何保持 float 顶部栏?

javascript - Ag-Grid - 如何为最后一行设置边框底部

wpf - 设置 WPF DataGrid 样式以在有 0 个项目时显示红色边框

c# - 自定义 DataTemplate - 控件不占用完整的网格列宽

c# - WPF 中 IP 地址的文本框验证