此时无法修改此节点的逻辑子节点,因为树遍历正在进行中

发布于 2024-12-05 09:27:26 字数 2426 浏览 0 评论 0原文

我有两个 wpf 工具包图表,一个是饼图,第二个是条形图系列,

我有仅在表单加载时调用的方法,

 chartGuest.DataContext = null;
            List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();
            HProDataContext db = new HProDataContext();

            var _RoomTypes = (from d in db.roomtypes select d.roomtype1).ToList();
            var _RoomTypeID = (from d in db.roomtypes select d.id).ToList();
            int count = 0;

            for (int i = 0; i < _RoomTypeID.Count; i++)
            {
                count = Convert.ToInt32((from d in db.actions where d.room.roomtypeid == _RoomTypeID[i] select d.id).Count());
                valueList.Add(new KeyValuePair<string, int>(_RoomTypes[i], count));

            }
            chartGuest.DataContext = valueList;

它给出了这样的错误:此时无法修改此节点的逻辑子节点,因为正在进行树遍历。

相同的代码在饼系列图表上效果很好。

这是我的图表:

 <charting:Chart x:Name="chartRoomType" Width="402" Height="255" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="15,275,0,0">
            <charting:Chart.Series>
                <charting:PieSeries ItemsSource="{Binding}" DependentValuePath="Value" IndependentValuePath="Key" Title="Room Types" IsSelectionEnabled="True" />
            </charting:Chart.Series>
                    </charting:Chart>
                    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="314,292,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click"  />
                    <TextBlock Height="23" HorizontalAlignment="Left" Margin="28,296,0,0" Name="textBlock4" Text="Room Types" VerticalAlignment="Top" />
                        <charting:Chart x:Name="chartGuest" Height="269" VerticalAlignment="Top" Margin="6,0" Title="Guests">
                            <charting:Chart.Series>
                                <charting:BarSeries ItemsSource="{Binding}" DependentValuePath="Value" IndependentValuePath="Key" Title="Room Types" IsSelectionEnabled="True" />
                            </charting:Chart.Series>
                        </charting:Chart>

有人可以帮助我吗? PS我发现了这个问题,但没有帮助 什么是由于树遍历正在进行中,因此无法修改此节点的逻辑子节点是什么意思?

I have two wpf tool kit charts one is pie chart and second bar series

i have method which i call only on form load

 chartGuest.DataContext = null;
            List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();
            HProDataContext db = new HProDataContext();

            var _RoomTypes = (from d in db.roomtypes select d.roomtype1).ToList();
            var _RoomTypeID = (from d in db.roomtypes select d.id).ToList();
            int count = 0;

            for (int i = 0; i < _RoomTypeID.Count; i++)
            {
                count = Convert.ToInt32((from d in db.actions where d.room.roomtypeid == _RoomTypeID[i] select d.id).Count());
                valueList.Add(new KeyValuePair<string, int>(_RoomTypes[i], count));

            }
            chartGuest.DataContext = valueList;

It gaves such error : Cannot modify the logical children for this node at this time because a tree walk is in progress.

The same code works great on pie series chart.

This is my charts:

 <charting:Chart x:Name="chartRoomType" Width="402" Height="255" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="15,275,0,0">
            <charting:Chart.Series>
                <charting:PieSeries ItemsSource="{Binding}" DependentValuePath="Value" IndependentValuePath="Key" Title="Room Types" IsSelectionEnabled="True" />
            </charting:Chart.Series>
                    </charting:Chart>
                    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="314,292,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click"  />
                    <TextBlock Height="23" HorizontalAlignment="Left" Margin="28,296,0,0" Name="textBlock4" Text="Room Types" VerticalAlignment="Top" />
                        <charting:Chart x:Name="chartGuest" Height="269" VerticalAlignment="Top" Margin="6,0" Title="Guests">
                            <charting:Chart.Series>
                                <charting:BarSeries ItemsSource="{Binding}" DependentValuePath="Value" IndependentValuePath="Key" Title="Room Types" IsSelectionEnabled="True" />
                            </charting:Chart.Series>
                        </charting:Chart>

Can anyone help me?
P.s. i found this question but it was unhelpful
What does Cannot modify the logical children for this node at this time because a tree walk is in progress mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毅然前行 2024-12-12 09:27:26

使用 DataPointSeries.ItemsSource 绑定到数据上下文。

假设这是窗口面板的 XAML,其中数据网格和图表控件共享一个公共列表作为 ItemsSource

  <StackPanel>
    <tk:DataGrid MaxHeight="200" AutoGenerateColumns="False"
                 ItemsSource="{Binding}"
                 IsReadOnly="True">
        <tk:DataGrid.Columns>
            <tk:DataGridTextColumn Header="Key"
                                   Binding="{Binding Key, Mode=OneWay}"/>
            <tk:DataGridTextColumn Header="Value"
                                   Binding="{Binding Value, Mode=OneWay}"/>
        </tk:DataGrid.Columns>
    </tk:DataGrid>

    <charting:Chart MaxHeight="300"
                    Title="Title"
                    LegendTitle="Legend"
                    Name="Chart1">
        <charting:AreaSeries DependentValuePath="Value"
                             IndependentValuePath="Key"
                             Background="Red" >
            <charting:DataPointSeries.ItemsSource>
                <Binding BindsDirectlyToSource="True"/>
            </charting:DataPointSeries.ItemsSource>
        </charting:AreaSeries>
    </charting:Chart>

    <Button Content="Change DataGrid and Chart Data" Click="Button_Click"/>
</StackPanel>

在后面的代码中,我们重置窗口的数据上下文...

    private List<KeyValuePair<int, int>> list1;

    public Window1()
    {
        InitializeComponent();

        list1 = new List<KeyValuePair<int, int>>();
        var random = new Random();
        for(int i = 0; i < 1000; i++)
        {
            list1.Add(new KeyValuePair<int, int>(i, random.Next()));
        }

        this.DataContext = list1;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        list1 = new List<KeyValuePair<int, int>>();
        var random = new Random();
        for (int i = 0; i < 1000; i++)
        {
            list1.Add(new KeyValuePair<int, int>(i, random.Next()));
        }

        this.DataContext = list1;
    }

每次单击按钮时,图表都会刷新而不会出现错误。

让我知道这是否有帮助。

Use DataPointSeries.ItemsSource to bind to the data context.

Assuming that this is XAML of your your window panel that has the datagrid and charting control sharing a common list as ItemsSource..

  <StackPanel>
    <tk:DataGrid MaxHeight="200" AutoGenerateColumns="False"
                 ItemsSource="{Binding}"
                 IsReadOnly="True">
        <tk:DataGrid.Columns>
            <tk:DataGridTextColumn Header="Key"
                                   Binding="{Binding Key, Mode=OneWay}"/>
            <tk:DataGridTextColumn Header="Value"
                                   Binding="{Binding Value, Mode=OneWay}"/>
        </tk:DataGrid.Columns>
    </tk:DataGrid>

    <charting:Chart MaxHeight="300"
                    Title="Title"
                    LegendTitle="Legend"
                    Name="Chart1">
        <charting:AreaSeries DependentValuePath="Value"
                             IndependentValuePath="Key"
                             Background="Red" >
            <charting:DataPointSeries.ItemsSource>
                <Binding BindsDirectlyToSource="True"/>
            </charting:DataPointSeries.ItemsSource>
        </charting:AreaSeries>
    </charting:Chart>

    <Button Content="Change DataGrid and Chart Data" Click="Button_Click"/>
</StackPanel>

In code behind we reset the data context of the window ....

    private List<KeyValuePair<int, int>> list1;

    public Window1()
    {
        InitializeComponent();

        list1 = new List<KeyValuePair<int, int>>();
        var random = new Random();
        for(int i = 0; i < 1000; i++)
        {
            list1.Add(new KeyValuePair<int, int>(i, random.Next()));
        }

        this.DataContext = list1;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        list1 = new List<KeyValuePair<int, int>>();
        var random = new Random();
        for (int i = 0; i < 1000; i++)
        {
            list1.Add(new KeyValuePair<int, int>(i, random.Next()));
        }

        this.DataContext = list1;
    }

Everytime you click the button the chart refreshes without error.

Let me know if this helps.

君勿笑 2024-12-12 09:27:26

我在不同的情况下收到了相同的异常。我使用的代码收集现有 UIElements 并创建新的 UIElement 并将第一个设置为第二个 UIElement 的内容。此外,它从现有 UIElement 中获取属性值并将其设置为新 UIElement 的不同属性的值。

foreach(UIElement insideElement in uiElementCollection)
{
    var outsideElement = new TabItem
    {
         Content = insideElement
         ,Header = insideElement.Title
    }
}

但在这种情况下,内部属性的 Title 可以是 Binding,但上面的代码仅复制值,并且当 insideElement.Title 绑定到某个源时,outsideElement.Header 不会反映更改。
所以我更改了代码将 HeaderProperty 绑定到 insideElement 的 TitleProperty:

foreach(UIElement insideElement in uiElementCollection)
{
    var outsideElement = new TabItem
    {
         Content = insideElement
    }

    outsideElement.SetBinding(HeaderedContentControl.HeaderProperty,
                        new Binding
                        {
                            Source = insideElement,
                            Path = new PropertyPath(MyUIElement.TitleProperty)
                        });
}

但是当我执行上面的代码时,我收到了

System.InvalidOperationException:此时无法修改此节点的逻辑子节点,因为树遍历正在进行中。

我发现我无法将一个依赖属性绑定到存储在 Content 属性内的另一个依赖属性。

我尝试获取innerUIElement 的绑定表达式并将其设置为outsideUIElement 的HeaderProperty 的新绑定。

foreach(UIElement insideElement in uiElementCollection)
{
    var outsideElement = new TabItem
    {
         Content = insideElement
    }

    BindingExpression titleBindingExpression = pageControl.GetBindingExpression(MyUIElement.TitleProperty);
        if (titleBindingExpression != null)
            {                            
                tabItem.SetBinding(HeaderedContentControl.HeaderProperty, new Binding { Source = titleBindingExpression.DataItem, Path = titleBindingExpression.ParentBinding.Path });

            }
            else
            {
                tabItem.Header = innerUIElement.Title;
            }
}

现在,外部 UI 元素绑定到相同的源,并且 WPF 演示器不会抛出异常。我的问题解决了。

I've received the same exception in different situation. I work with code which takes collection of existing UIElements and create new UIElement and set first one as Content of second one. Aditionally, it takes property value from existing UIElement and set it as value of different property of new UIElement.

foreach(UIElement insideElement in uiElementCollection)
{
    var outsideElement = new TabItem
    {
         Content = insideElement
         ,Header = insideElement.Title
    }
}

But in this case Title of inside property can be a Binding, but code above copies only value and when insideElement.Title is bounded to some source, outsideElement.Header doesn't reflect change.
So I changed code to bind HeaderProperty to TitleProperty of insideElement:

foreach(UIElement insideElement in uiElementCollection)
{
    var outsideElement = new TabItem
    {
         Content = insideElement
    }

    outsideElement.SetBinding(HeaderedContentControl.HeaderProperty,
                        new Binding
                        {
                            Source = insideElement,
                            Path = new PropertyPath(MyUIElement.TitleProperty)
                        });
}

But when I executed code above, I received

System.InvalidOperationException: Cannot modify the logical children for this node at this time because a tree walk is in progress.

I found that I can't bind one dependency property to another one, which is stored inside Content property.

I tried to get binding expression of innerUIElement and set it as new binding to HeaderProperty of outsideUIElement.

foreach(UIElement insideElement in uiElementCollection)
{
    var outsideElement = new TabItem
    {
         Content = insideElement
    }

    BindingExpression titleBindingExpression = pageControl.GetBindingExpression(MyUIElement.TitleProperty);
        if (titleBindingExpression != null)
            {                            
                tabItem.SetBinding(HeaderedContentControl.HeaderProperty, new Binding { Source = titleBindingExpression.DataItem, Path = titleBindingExpression.ParentBinding.Path });

            }
            else
            {
                tabItem.Header = innerUIElement.Title;
            }
}

Now, outer UI Element is binded to same source and WPF presenter doesn't throw Exception. My problem is solved.

昔梦 2024-12-12 09:27:26

我知道这已经有一段时间了,但为了防止其他人遇到它,我找到了另一种方法。将图表放入 StackPanel 中,创建时可见性为“折叠”(“隐藏”不起作用)。然后在第一次设置 DataContext 后将可见性设置为“Visible”(我每次都会这样做,但在第一次之后它是多余的)。

我的经验与我看到的报告一致,问题是在有一个空的 DataContext 之后设置一个非空的 DataContext 时发生的错误;但显然,如果图表折叠,它不会执行“树行走”。

I know this has been a while, but in case anyone else comes across it I found another way around it. Put the chart into a StackPanel, created with Visibility of "Collapsed" ("Hidden" DOES NOT WORK). Then set visibility to "Visible" after setting DataContext the first time (I'm doing it every time, but after the first it's redundant).

My experience agrees with reports I've seen that the problem is a bug that occurs when setting a non-empty DataContext after having had an empty one; but apparently it doesn't do the "tree walk" if the chart is collapsed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文