绑定更新将新闻系列添加到WPF工具包图表(而不是替换/更新系列) [英] Binding update adds news series to WPF Toolkit chart (instead of replacing/updating series)
问题描述
我目前正在我的应用程式重新编码一个条形图,以利用图表 /rel =nofollow noreferrer> WPF Toolkit 。使用MVVM,我将我的图表中的 ColumnSeries
的 ItemsSource
绑定到我的viewmodel上的属性。以下是相关的XAML:
< charting:Chart>
< charting:ColumnSeries ItemsSource ={Binding ScoreDistribution.ClassScores}
IndependentValuePath =ClassNameDependentValuePath =Score/>
< / charting:Chart>
和viewmodel上的属性:
//注意:viewmodel派生自Josh Smith的BindableObject
public class ExamResultsViewModel:BindableObject
{
// ...
$ b b private ScoreDistributionByClass _scoreDistribution;
public ScoreDistributionByClass ScoreDistribution
{
get
{
return _scoreDistribution;
}
set
{
if(_scoreDistribution == value)
{
return;
}
_scoreDistribution = value;
RaisePropertyChanged(()=> ScoreDistribution);
}
}
但是,当我更新 ScoreDistribution
属性(通过将其设置为新的 ScoreDistribution
对象),图表会获取一个新的ScoreDistribution)以及保持原系列(基于以前的ScoreDistribution)。
为了说明这一点,下面是一些截图,显示更新前的图表(在 ScoreDistribution.ClassScores
中的单个数据点)和之后(现在在 ScoreDistribution.ClassScores
):
现在,我意识到还有其他方法可以做到这一点(例如改变原来 ScoreDistribution
object而不是完全替换它),但我不明白为什么它的错误在当前的形式。任何人都可以帮助?
结果是问题是由图表中的更新频率 ,而不是整个系列正在被替换的事实;将数据绑定属性更改为 ObservableCollection
没有什么区别。
最后,我们更改了代码,基础数据更改之间的延迟,这些更改反映在ViewModel的绑定集合属性中。虽然它在一定程度上取决于运行应用程序的机器的速度,但我们已经确定了在最后一个底层数据更改和ViewModel属性更新之间的0.5秒延迟。这可以防止图表更新超过每0.5秒,它似乎做的工作。
在某些时候,我实际上通过WPFToolkit代码,看看什么我可以解决它,但现在,这个解决方法值得注意。
I'm currently recoding a bar chart in my app to make use of the Chart
class in the WPF Toolkit. Using MVVM, I'm binding the ItemsSource
of a ColumnSeries
in my chart to a property on my viewmodel. Here's the relevant XAML:
<charting:Chart>
<charting:ColumnSeries ItemsSource="{Binding ScoreDistribution.ClassScores}"
IndependentValuePath="ClassName" DependentValuePath="Score"/>
</charting:Chart>
And the property on the viewmodel:
// NB: viewmodel derived from Josh Smith's BindableObject
public class ExamResultsViewModel : BindableObject
{
// ...
private ScoreDistributionByClass _scoreDistribution;
public ScoreDistributionByClass ScoreDistribution
{
get
{
return _scoreDistribution;
}
set
{
if (_scoreDistribution == value)
{
return;
}
_scoreDistribution = value;
RaisePropertyChanged(() => ScoreDistribution);
}
}
However, when I update the ScoreDistribution
property (by setting it to a new ScoreDistribution
object), the chart gets an additional series (based on the new ScoreDistribution) as well as keeping the original series (based on the previous ScoreDistribution).
To illustrate this, here are a couple of screenshots showing the chart before an update (with a single data point in ScoreDistribution.ClassScores
) and after it (now with 3 data points in ScoreDistribution.ClassScores
):
Now, I realise there are other ways I could be doing this (e.g. changing the contents of the original ScoreDistribution
object rather than replacing it entirely), but I don't understand why it's going wrong in its current form. Can anyone help?
Turns out the problem was triggered by the frequency of updates in the chart, not the fact that the entire series was being replaced; changing the databound property to an ObservableCollection
made no difference.
In the end, we changed our code to include a delay between changes to the underlying data and those changes being reflected in the ViewModel's bound collection property. While it does depend to an extent on the speed of the machine on which the app is running, we've settled on a 0.5sec delay between the last underlying data change and the ViewModel property updating. This prevents the chart being updated more than every 0.5secs and it seems to do the job.
At some point, I'll actually go through the WPFToolkit code and see what I can do to fix it, but for now, this workaround's worth noting.
这篇关于绑定更新将新闻系列添加到WPF工具包图表(而不是替换/更新系列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!