What does Cannot modify the logical children for this node at this time because a tree walk is in progress mean?

自古美人都是妖i 提交于 2019-11-30 04:47:31

After playing around more, I think this is a bug in the Silverlight charting toolkit.

The following code causes a reproduceable crash.

int runCount = 0;
        private void bindChart(string searchString)
        {
           List<KeyValuePair<DateTime, int>> dataEmpty = new List<KeyValuePair<DateTime, int>>();

            List<KeyValuePair<DateTime, int>> dataFilled = new List<KeyValuePair<DateTime, int>>();
            dataFilled.Add(new KeyValuePair<DateTime, int>(DateTime.Today, 1));
            if (runCount == 0)
            {
                Chart1.DataContext= dataEmpty;
            }
            else
            {
                Chart1.DataContext = dataFilled;
            }
            runCount++;

        }

XAML:

<charting:Chart Grid.Row="0"
    Title="Title"
    LegendTitle="Legend" Name="Chart1" Grid.RowSpan="2">
            <charting:AreaSeries ItemsSource="{Binding}"

                                       DependentValuePath="Value"

                                       IndependentValuePath="Key"

                                       Background="Red" />


        </charting:Chart>

This will fail on the second call to bindChart.

Alfie

SOLVED!

The problem: I want to update my chart on the GUI every time some data is changed.

 myChart.DataContext = MTFdata;

when i do this i gett the error: Cannot modify the logical children for this node at this time because a tree walk is in progress

How I solved it:

Insted of this:

 <chartingToolkit:LineSeries   DependentValuePath="Key" 
                                                    IndependentValuePath="Value" 
                                                    ItemsSource="{Binding}"
                                                    IsSelectionEnabled="False"
                                                                             >

Use This:

 <chartingToolkit:LineSeries   DependentValuePath="Key" 
                                                    IndependentValuePath="Value" 
                                                    ItemsSource="{Binding}"
                                                    DataContext="{Binding}"
                                                    IsSelectionEnabled="False"
                                                                             >

Use both ItemsSource="{Binding}" and DataContext="{Binding}"

Hope this helps!

Hej,

I've just had the error, and fixed it as well. The error also accoured when setting the datacontext.

I found that I had a selectionchanged-subscription on the list that was having its datacontext set. In this selectionchanged i was altering another property with notification support, which had a visual element binding on it.

Solved the problem by using the dispatcher for setting the property.

So try look for subscriptions on changes...

Apparently, some operation is being made that is changing a collection while it's being used. That's a big no-no.

Without analyzing the code further, that's all I can say.

Instead of resetting DataContext reusing via observable collection also works...

    int runCount = 0;

    private void bindChart()
    {
        ObservableCollection<KeyValuePair<DateTime, int>> data = new ObservableCollection<KeyValuePair<DateTime, int>>();

        if (runCount == 0)
        {
            this.DataContext = dataEmpty;
        }
        else
        {
            var de = this.DataContext as ObservableCollection<KeyValuePair<DateTime, int>>;
            de.Clear();
            for (var i = 0; i < (new Random(DateTime.Now.Second)).Next(100); i++)
            {
                de.Add(new KeyValuePair<DateTime, int>(DateTime.Today.AddDays(i), i));
            }
        }

        runCount++;
    } 

I just had this issue with the WPF charting toolkit. Everyone said not to bind to the DataContext and bind directly to the ItemsSource but I already was binding to the ItemsSource and still got the error. In my case I was binding to the SelectedItem of a ListBox or TreeView. The only thing I could get to work was to manually bind to the ItemsSource when the SelectedItemChanged() event fires.

private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    if (TreeView.SelectedItem != null)
        LineSeries.ItemsSource = (TreeView.SelectedItem as MyObject).Items;
}

I had this issue when a parent control (AvalonDock) triggered the child user control to reload: the user control would fire its "Loaded" event and my application would crash.

I removed the Loaded event handler from the user control and used the Initialized event instead. That fixed my problem although I'm not totally sure why.

Siderite

Here is my research on it.

Mirror of said link on WaybackMachine.

It is a WPF Exception thrown by Add/RemoveLogicalChild methods in FrameworkElement and FrameworkContentElement when a tree walk is in progress. The boring details of it are in the blog post, but the bottom line is that you can't do more than test for it then catch it where it occurs. The exception should be a relatively rare occurence.

I have the same problem. I am not using any background threads though. I just have a slider and on the ValueChanged event for the slider, I am recalculating and resetting the DataContext.

For example, A burn down chart. As the initial value changes, the graph updates automatically (essentially the Y axis of line series).

Changing the datacontext worked in the beta version of silverlight 3. Doesn't work in the release version. The above comment about setting (Chart1.Series[0] as DataPointSeries).ItemsSource works for me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!