2

我正在尝试更新图表并在那里绘制一些散点。为此,我正在使用BackgroundWorker类。它确实有效。但我注意到,一旦我向我的 Point 类添加颜色并想要显示不同颜色的点,它就会崩溃。为什么?有任何想法吗?

    public class ChartData
    {
        private readonly Brush Red = new SolidColorBrush(Colors.Red);
        private readonly Brush Orange = new SolidColorBrush(Colors.Orange);
        private readonly Brush Green = new SolidColorBrush(Colors.Green);

        public ChartData(double x, double y)
        {
            this.XValue = x;
            this.YValue = y;
        }

        public double XValue { get; set; }
        public double YValue { get; set; }

        public Brush Brush{ get; set;}
    }

    <telerik:ScatterPointSeries XValueBinding="XValue" 
                            YValueBinding="YValue" 
                            ItemsSource="{Binding Data}" >
        <telerik:ScatterPointSeries.PointTemplate>
            <DataTemplate>
                <Ellipse Width="10"
             Height="10"
             Fill="{Binding DataItem.Brush}"/>
            </DataTemplate>
        </telerik:ScatterPointSeries.PointTemplate>
    </telerik:ScatterPointSeries>

例外:Must create DependencySource on same Thread as the DependencyObject.

4

1 回答 1

1

如消息本身所述,您不能DependencySource从另一个线程修改(添加、创建、删除)。您可以从同一个线程修改它,在您的情况下是 UI 线程。

作为一种出路,你可以把代码modifying dependency source on UI thread dispatcher

App.Current.Dispatcher.Invoke((Action)delegate()
            {
                // Code here for updating Dependency Source.
            });
于 2013-03-16T19:36:53.273 回答