为什么我的ItemsControl性能如此之慢?

时间:2016-04-14 12:05:18

标签: c# wpf performance xaml itemscontrol

我有一个看起来像这样的ItemsControl:

<ItemsControl ItemsSource="{Binding CombinedPieChartData}"
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Canvas>
                <Path Fill="{Binding Color}">
                    <Path.Data>
                        <GeometryGroup>
                            <PathGeometry>
                                <PathFigure StartPoint="{Binding FirstPoint1}">
                                    <PathFigure.Segments>
                                        <LineSegment Point="{Binding SecondPoint1}"/>
                                    </PathFigure.Segments>
                                </PathFigure>
                            </PathGeometry>
                        </GeometryGroup>
                    </Path.Data>
                </Path>
                <Path Fill="{Binding Color}">
                    <Path.Data>
                        <GeometryGroup>
                            <PathGeometry>
                                <PathFigure StartPoint="{Binding Center}">
                                    <PathFigure.Segments>
                                        <LineSegment Point="{Binding FirstPoint}"/>
                                        <ArcSegment Point="{Binding SecondPoint}" Size="{Binding PieSize}" IsLargeArc="{Binding IsLargeArc}" SweepDirection="Clockwise"/>
                                        <LineSegment Point="{Binding Center}"/>
                                    </PathFigure.Segments>
                                </PathFigure>
                            </PathGeometry>
                        </GeometryGroup>
                    </Path.Data>
                </Path>
                <TextBlock TextAlignment="{Binding TextAlign}" Width="50" Text="{Binding Percentage}" FontSize="14" Canvas.Top="{Binding Top}" Canvas.Left="{Binding Left}" Background="{Binding Color}"/>
            </Canvas>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

实际上会按预期显示饼图,但启动速度非常慢,并将大量错误抛出到表单的调试输出中:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=FirstPoint1; DataItem='PieChartDataPoint' (HashCode=60223391); target element is 'PathFigure' (HashCode=35353884); target property is 'StartPoint' (type 'Point')

它为FirstPoint1,SecondPoint1,Center,FirstPoint,SecondPoint,PieSize和IsLargeArc执行此操作

基本上是Path.Data中的任何绑定。我无法在线找到任何帮助,但试图通过将此行添加到Bindings来设置绑定的相对来源

RelativeSource={RelativeSource 
FindAncestor, AncestorType={x:Type ItemsControl}}

这会阻止显示上述错误但反过来吐出这些错误:

'System.Windows.Data Error: 4 : Cannot find source for binding with reference RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=FirstPoint1; DataItem=null; target element is 'PathFigure' (HashCode=38648509); target property is 'StartPoint' (type 'Point')

它会停止正确显示控件。

0 个答案:

没有答案