重置数据时如何重置WPF工具包图表调色板
我在 WPF 用户控件上有一个饼图,其数据定期更改,但是我并不是每次都实例化新的图表控件,只是清除 ItemsSource 中的数据,然后插入新值。
每次刷新值时,调色板都会继续滚动其颜色选择。
图表颜色选择总是以相同的颜色选择开始(首先是红色,然后是蓝色等),我希望能够告诉图表在每次重置数据源时从头开始重新启动其颜色选择,而不是获取每次我清除并重置数据项时都会有不同的颜色。
我每次都尝试创建 ObservableCollection 的新实例,但这没有任何区别。
I have a pie chart on a WPF user control whose data changes periodically, however I am not instantiating a new chart control each time, just clearing the data in the ItemsSource and then inserting new values.
Each time the values get refreshed, the colour palate continues rolling through its colour selections.
The chart colour selection always starts off with the same colour selections (first is red, then blue etc), I would like to be able to tell the chart to restart its colour selection from scratch each time I reset the data source, instead of getting different colours everytime I clear the and reset the data items.
I tried creating a new instance of the ObservableCollection each time but that did not make any difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,我找到了另一个解决方案。也许这不是更好的方法,但它确实有效。
我向 PieSeries 上的对象绑定添加了一个 int 属性,该属性表示 ObservableCollection 中元素的索引。
然后我创建了一个 ResourceDictionaryCollection,其中包含调色板的所有默认颜色:
我添加了一个转换器,该转换器返回 ResourceDictionaryCollection 指定索引处的颜色。
我在 Xaml 中使用了所有这些元素,如下所示:
我希望这个答案可以帮助你。
I've got the same problem, and I found another solution. Maybe it's not the better one, but it works.
I added an int property to my object binding on the PieSeries, who represent the element's index in the ObservableCollection.
Then I created a ResourceDictionaryCollection which contains all the default colors of the Palette:
I added a converter that returns the color at the specified index of ResourceDictionaryCollection.
And i used all this elements in the Xaml like this:
I hope that answer can help you.
如果您不想向数据和转换器添加其他属性,则可以使用反射。
为了帮助轻松地呈现数据,wpftoolkit 数据系列有一个可继承的
ResourceDictionaryDispenser
,负责在每次调用时提供一组不同的资源字典。每个DataPointSeries
中的ResourceDictionaryDispenser
从Chart
对象中获取自己的枚举器。不幸的是,
Chart
中的ResourceDictionaryDispenser
属性是私有的,它的Reset()
方法也是私有的。不过,您可以使用以下代码:存储对方法和资源分配器的引用后,您应该避免将数据源与系列的
ItemsSource
直接绑定。拦截您的数据更新(可能使用INotifyPropertyChanged.PropertyChanged
事件)并按如下方式操作:经过测试且 100% 有效。
XAML 是这样的:
您也应该添加这些
using
。If you don't want to add another property to your data and a converter, you can use reflection.
To aid in easy data presentation, wpftoolkit data series have an inheritable
ResourceDictionaryDispenser
in charge with providing a different set of resource dictionaries every time it's called. TheResourceDictionaryDispenser
in eachDataPointSeries
gets its own enumerators from the one in theChart
object.Too bad the
ResourceDictionaryDispenser
property inChart
is private, as is itsReset()
method. You can use the following code, though:Once you stored a reference to the method and the resource dispenser, you should avoid direct binding your data source with the series'
ItemsSource
. Intercept instead your data update (maybe with theINotifyPropertyChanged.PropertyChanged
event) and act like this:tested and 100% working.
The XAML is like this:
you should add these
using
s too.