【问题标题】:DependencyProperty Setter called After OnItemsSourceChanged在 OnItemsSourceChanged 之后调用的 DependencyProperty Setter
【发布时间】: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


【解决方案1】:

我想出了一个解决方案来延迟初始化,直到列被加载,然后所有属性都可用。

public class SpreadsheetCellTemplateListing : DependencyObject
{
    public DataTemplate structTemplate { get; set; }
    public DataTemplate atomicTemplate { get; set; }
}

internal class SpreadsheetDataGrid : DataGrid
{
    private bool InitializeColumnsOnLoad = false;

    public SpreadsheetDataGrid()
    {
        Loaded += OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        if (InitializeColumnsOnLoad)
        {
            InitializeColumns();
        }
    }

    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);
        if (!IsLoaded)
        {
            InitializeColumnsOnLoad = true;
        }
        else
        {
            InitializeColumns();
        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-02-25
    • 1970-01-01
    • 2020-01-18
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多