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

时间:2011-09-17 20:26:18

标签: wpf charts wpftoolkit

我有两个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>

任何人都可以帮助我吗? 附:我发现了这个问题,但它没有用 What does Cannot modify the logical children for this node at this time because a tree walk is in progress mean?

3 个答案:

答案 0 :(得分:1)

使用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;
    }

每次点击该按钮,图表都会刷新而不会出错。

如果有帮助,请告诉我。

答案 1 :(得分:0)

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

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

但是在这种情况下,内部属性的标题可以是绑定,但上面的代码只复制值,当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演示者不会抛出异常。我的问题解决了。

答案 2 :(得分:0)

我知道这已经有一段时间了,但是如果有其他人遇到它我发现了另一种方法。将图表放入StackPanel,使用“Collapsed”的可见性创建(“隐藏”不工作)。然后在第一次设置DataContext后将可见性设置为“Visible”(我每次都这样做,但是在第一次设置后是多余的)。

我的经验同意报告我已经看到问题是在一个空的DataContext之后设置非空DataContext时发生的错误;但如果图表崩溃,显然它不会进行“树行走”。