我想在修改列表中的元素后更新视图。目前,当我修改列表中的项目时,系列会正确更新,但视图不会更新,或者只是部分更新。我应该提到的是,被修改的属性是颜色,如果调整我的窗口大小,视图会正确更新。
我的对象:
`public class cDay : ObservableObject
{
public cDay()
{
byte[] abyColors = cColorConverter.HexToBytes("#A5A5A5");
oColor = new SolidColorPaint(new SKColor(abyColors[0], abyColors[1], abyColors[2]));
}
private SolidColorPaint color;
public SolidColorPaint oColor
{
get => color;
set
{
color = value;
OnPropertyChanged();
}
}
private double value;
public double Value
{
get => value;
set
{
this.value = value;
OnPropertyChanged();
}
}
private string label;
public string Label
{
get => label;
set
{
label = value;
OnPropertyChanged();
}
}
}`
字符串
我的视图模型:
`internal class ViewModel : ObservableObject
{
public ViewModel()
{
InitColor();
data = GetData();
InitSeries();
}
private static SolidColorPaint oColorRed;
private static SolidColorPaint oColorGreen;
private static SolidColorPaint oColorDefault;
private static SolidColorPaint oColorBlack;
private ObservableCollection<cDay> data;
private ISeries[] _Series;
public ISeries[] Series
{
get
{
return _Series;
}
set
{
_Series = value;
OnPropertyChanged();
}
}
private ObservableCollection<cDay> GetData()
{
ObservableCollection<cDay> data = new ObservableCollection<cDay>()
{
new cDay() {Label = "J1", Value = 5 },
new cDay() {Label = "J2", Value = 5 },
new cDay() {Label = "J3", Value = 5 },
new cDay() {Label = "J4", Value = 5 },
new cDay() {Label = "J5", Value = 5 },
new cDay() {Label = "J6", Value = 5 }
};
return data;
}
private void InitColor()
{
string sHexColorRed = "#FF0000";
string sHexColorBlack = "#000000";
string sHexColorGreen = "#2AFF00";
string sHexColorDefault = "#A5A5A5";
byte[] abyColors = cColorConverter.HexToBytes(sHexColorRed);
oColorRed = new SolidColorPaint(new SKColor(abyColors[0], abyColors[1], abyColors[2]));
abyColors = cColorConverter.HexToBytes(sHexColorDefault);
oColorDefault = new SolidColorPaint(new SKColor(abyColors[0], abyColors[1], abyColors[2]));
abyColors = cColorConverter.HexToBytes(sHexColorGreen);
oColorGreen = new SolidColorPaint(new SKColor(abyColors[0], abyColors[1], abyColors[2]));
abyColors = cColorConverter.HexToBytes(sHexColorBlack);
oColorBlack = new SolidColorPaint(new SKColor(abyColors[0], abyColors[1], abyColors[2]));
}
private void InitSeries()
{
Series = null;
var seriesCollection = data.AsPieSeries(
(city, series) =>
{
series.Name = city.Label;
series.InnerRadius = 60;
series.Stroke = oColorBlack;
series.Fill = city.oColor;
series.Mapping = (cityMapper, point) =>
{
point.Coordinate = new Coordinate(point.Index, cityMapper.Value);
};
series.DataPointerDown += Series_DataPointerDown;
});
Series = seriesCollection.ToArray();
}
private void Series_DataPointerDown(IChartView chart, IEnumerable<ChartPoint<cDay,
DoughnutGeometry, LabelGeometry>> points)
{
foreach (var point in points)
{
string sOldName = point.Model?.Label;
UpdateSerie(sOldName);
}
}
private void UpdateSerie(string sName)
{
foreach (cDay item in data)
{
if (item.Label == sName)
{
SKColor color = item.oColor.Color;
if (color.ToString().ToUpper() == "#FFFF0000") // Rouge
{
item.oColor = oColorGreen;
}
else if (color.ToString().ToUpper() == "#FF2AFF00") //Vert
{
item.oColor = oColorDefault;
}
else if (color.ToString().ToUpper() == "#FFA5A5A5") // blanc default
{
item.oColor = oColorRed;
}
}
}
InitSeries();
}
}`
型
我的观点
`
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<lvc:PieChart Series="{Binding Series,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
EasingFunction="{x:Null}"/>
<Grid Grid.Column="1"
Background="AliceBlue">
</Grid>
</Grid>`
型
谢谢您的帮助。
1条答案
按热度按时间pexxcrt21#
代码的问题如下:你正在用ISeries的Array绑定PieChart的系列,而不是SeriesCollection(LiveCharts包的专有)。
所以,要让你的代码工作,你必须改变这一点:
字符串
变成这样:
型
你可以找到所有你需要的关于SeriesCollection here的信息(注意本教程的第3点)