【发布时间】:2015-01-16 10:53:28
【问题描述】:
我需要在我的 ItemsSource 更改时访问一些数据,以便我可以正确生成列并在我的 DataGrid 中绑定。为此,我需要绑定不同的模板并选择它们(不幸的是 CellTemplateSelector 在这种情况下对我不起作用。)
在下面的代码中,TemplateListing 的设置器似乎在 OnItemsSourceChanged 之后被调用。谁能解释一下为什么或如何解决这个问题?
public class SpreadsheetCellTemplateListing : DependencyObject
{
public DataTemplate structTemplate { get; set; }
public DataTemplate atomicTemplate { get; set; }
}
class SpreadsheetDataGrid : DataGrid
{
public static readonly DependencyProperty TemplateListingProperty =
DependencyProperty.Register("TemplateListing", typeof(SpreadsheetCellTemplateListing), typeof(SpreadsheetCellTemplateListing), new PropertyMetadata(null));
public SpreadsheetCellTemplateListing TemplateListing
{
get { return (SpreadsheetCellTemplateListing)GetValue(TemplateListingProperty); }
set { SetValue(TemplateListingProperty, value); }
}
protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue)
{
base.OnItemsSourceChanged(oldValue, newValue);
//Template listing is NULL
}}
【问题讨论】:
-
你在
base.OnItemsSourceChanged(oldValue, newValue);987654322@之前有没有尝试过修改 -
不幸的是在这种情况下它为空
-
为什么它不应该为空? AFAIK,依赖属性分配的顺序是未定义的,尤其是在使用数据绑定时。您必须处理这两个属性的更改。
标签: c# wpf dependency-properties itemssource