C# Chartingtoolkit:饼图系列在更新后更改颜色

C# Chartingtoolkit:饼图系列在更新后更改颜色,c#,wpf,charts,observablecollection,C#,Wpf,Charts,Observablecollection,我使用一个饼图序列图来显示我添加到ObervableCollection的四个值的分配。一切正常,但当我更新ObervableCollection时,颜色的分配会发生变化(更新意味着我清除集合并添加新值,这可能会导致问题。但OCs不存在替换方法)。我还为饼图系列定义了标准颜色,不管赋值如何变化 这就是当我将第一个值填充到绑定到图表的ObservableCollection时,我的饼图系列的样子: 更新ObservaleCollection后,看起来如下所示: 正如您所看到的,这些值位于相同的

我使用一个饼图序列图来显示我添加到ObervableCollection的四个值的分配。一切正常,但当我更新ObervableCollection时,颜色的分配会发生变化(更新意味着我清除集合并添加新值,这可能会导致问题。但OCs不存在替换方法)。我还为饼图系列定义了标准颜色,不管赋值如何变化

这就是当我将第一个值填充到绑定到图表的ObservableCollection时,我的饼图系列的样子:

更新ObservaleCollection后,看起来如下所示:

正如您所看到的,这些值位于相同的位置。只有指定给这些值的颜色改变了它们的位置

这是我填写ObservableCollection的地方: 私有ObservableCollection>\u分配列表

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);
}
publicobservableCollection分配列表
{
获取{return\u allocationList;}
设置
{
_分配列表=值;
OnPropertyChanged(“分配列表”);
}
}
if(AllocationList!=null)
{
ObservableCollection TempAllocationList=CheckAllocation.GetAllocation(Value1List、Value2List、Value3List、Value4List);
int temp1Value=templocationlist[0]。Value+AllocationList[0]。Value;
int temp2Value=templocationlist[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,新操作(()=>
{
AllocationList.Clear();
添加(新的KeyValuePair(“Value1”,temp1Value));
添加(新的KeyValuePair(“Value2”,temp2Value));
添加(新的KeyValuePair(“Value3”,temp3Value));
添加(新的KeyValuePair(“Value4”,temp4Value));
}));
}
其他的
{
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>


谢谢你帮助我

你的猜测是正确的。这是listitems的清除和重新添加的问题。 一个没有clear的解决方案是获取项目的索引并用索引器替换它们:

var pvI=AllocationList.Select(x=>x.Key).ToList().IndexOf(“光伏”);
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(“wasserkaft”);
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,新操作(()=>
{
AllocationList[pvI]=新的KeyValuePair(“光伏”,tempVvalue);
AllocationList[pwkI]=新的KeyValuePair(“Windkraft”,tempWKValue);
AllocationList[bmvI]=新的KeyValuePair(“Biomasse”,tempBMValue);
AllocationList[wakI]=新的KeyValuePair(“wasserkaft”,tempWaKValue);
}));

你的猜测是正确的。这是listitems的清除和重新添加的问题。 一个没有clear的解决方案是获取项目的索引并用索引器替换它们:

var pvI=AllocationList.Select(x=>x.Key).ToList().IndexOf(“光伏”);
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(“wasserkaft”);
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,新操作(()=>
{
AllocationList[pvI]=新的KeyValuePair(“光伏”,tempVvalue);
AllocationList[pwkI]=新的KeyValuePair(“Windkraft”,tempWKValue);
AllocationList[bmvI]=新的KeyValuePair(“Biomasse”,tempBMValue);
AllocationList[wakI]=新的KeyValuePair(“wasserkaft”,tempWaKValue);
}));