我使用饼图系列图来显示我添加到ObervableCollection的四个值的分配。一切正常,但是当我更新ObervableCollection时,颜色的分配发生了变化(更新意味着我清除了Collection并添加了新值,可能会导致问题。但是没有OC的替换方法)。我也为饼图系列定义了标准颜色,但它的作用却有所改变。
当我将第一个值填充到绑定到图表的ObservableCollection时,这就是我的派系列的样子:
更新ObservableCollection后,它看起来像这样:
正如您所看到的,这些值处于相同的位置。只有分配给值的颜色才会改变它们的位置。
这是我填充ObservableCollection的地方: private ObservableCollection> _allocationList;
public ObservableCollection<KeyValuePair<string, int>> AllocationList
{
get { return _allocationList; }
set
{
_allocationList = value;
OnPropertyChanged("AllocationList");
}
}
if (AllocationList != null)
{
ObservableCollection<KeyValuePair<string, int>> TempAllocationList = CheckAllocation.GetAllocation(Value1List, Value2List, Value3List, Value4List);
int temp1Value = TempAllocationList[0].Value + AllocationList[0].Value;
int temp2Value = TempAllocationList[1].Value + AllocationList[1].Value;
int temp3Value = TempAllocationList[2].Value + AllocationList[2].Value;
int temp4Value = TempAllocationList[3].Value + AllocationList[3].Value;
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
AllocationList.Clear();
AllocationList.Add(new KeyValuePair<string, int>("Value1", temp1Value));
AllocationList.Add(new KeyValuePair<string, int>("Value2", temp2Value));
AllocationList.Add(new KeyValuePair<string, int>("Value3", temp3Value));
AllocationList.Add(new KeyValuePair<string, int>("Value4", temp4Value));
}));
}
else
{
AllocationList = CheckAllocation.GetAllocation(Value1List, Value2List, Value3List, Value4List);
}
这是我写的XAML:
<chartingToolkit:Chart>
<chartingToolkit:PieSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding AllocationList, UpdateSourceTrigger=PropertyChanged}">
<chartingToolkit:PieSeries.Palette>
<visualizationToolkit:ResourceDictionaryCollection>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="Green"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="#FF045704"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="#FF0AB60A"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="#FF246E24"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="#FF064006"/>
</Style>
</ResourceDictionary>
</visualizationToolkit:ResourceDictionaryCollection>
</chartingToolkit:PieSeries.Palette>
</chartingToolkit:PieSeries >
</chartingToolkit:Chart>
感谢您帮助我!
答案 0 :(得分:2)
你的猜测是正确的。这是清单和重新添加列表项的问题。 没有明确的一个解决方案是获取您的项目的索引并将其替换为索引器:
var pvI = AllocationList.Select(x => x.Key).ToList().IndexOf("Photovoltaik");
var pwkI = AllocationList.Select(x => x.Key).ToList().IndexOf("Windkraft");
var bmvI = AllocationList.Select(x => x.Key).ToList().IndexOf("Biomasse");
var wakI = AllocationList.Select(x => x.Key).ToList().IndexOf("Wasserkraft");
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
AllocationList[pvI] = new KeyValuePair<string, int>("Photovoltaik", tempPVValue);
AllocationList[pwkI] = new KeyValuePair<string, int>("Windkraft", tempWKValue);
AllocationList[bmvI] = new KeyValuePair<string, int>("Biomasse", tempBMValue);
AllocationList[wakI] = new KeyValuePair<string, int>("Wasserkraft", tempWaKValue);
}));