解决您的问题
问题在于使用返回 UI 控制元素的GetItem。使用对象列表视图,您应该根据对象执行所有操作,而不是更改实际的 Control 元素。基本上,您是在使用这种方法与 API 作斗争,而不是使用它。
我认为你应该使用RowFormatter来设置背景颜色:
OLVa.RowFormatter = (o) =>
{
if(o.RowObject == "B")
o.BackColor = Color.Pink;
};
重现您的问题
我能够使用以下(错误)代码重现您描述的问题。它会像您描述的那样产生闪烁:
public class MainForm : Form
{
public MainForm()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(284, 261);
this.Name = "MainForm";
this.ResumeLayout(false);
this.PerformLayout();
var OLVa = new ObjectListView();
OLVa.Dock = DockStyle.Fill;
OLVa.Columns.Add(new OLVColumn("Name", "ToString"));
this.Controls.Add(OLVa);
OLVa.AddObject("A");
OLVa.AddObject("B");
OLVa.AddObject("C");
Timer t = new Timer();
t.Interval = 100;
t.Tick += (s,e)=>OLVa.RefreshObject("B");
t.Start();
Timer t2 = new Timer();
t2.Interval = 200;
t2.Tick += (s,e)=>OLVa.GetItem(1).BackColor = Color.Pink;
t2.Start();
}
}
将第二个计时器更改为行格式化程序解决了问题。
根据条件更改行突出显示
调用RefreshObject 会自动触发RowFormatter 和方面,因此如果您想随时间更改行的状态(例如,按照您在评论中的要求),您可以执行以下操作:
public class MainForm : Form
{
public MainForm()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(300, 300);
this.Name = "MainForm";
this.ResumeLayout(false);
this.PerformLayout();
var OLVa = new FastObjectListView();
OLVa.Width = 250;
OLVa.Height = 250;
OLVa.Columns.Add(new OLVColumn("Done", "Done"));
OLVa.Columns.Add(new OLVColumn("Percent", "PercentComplete"));
this.Controls.Add(OLVa);
Video v = new Video();
OLVa.AddObject(v);
var t = new Timer();
t.Interval = 1000;
t.Start();
OLVa.RowFormatter = (s) => s.BackColor = ((Video) s.RowObject).Done ? Color.Green : Color.Red;
t.Tick += (s,e)=>
{
v.PercentComplete = Math.Min(v.PercentComplete += 10, 100);
if (v.PercentComplete == 100)
v.Done = true;
OLVa.RefreshObject(v);
};
}
private class Video
{
public bool Done { get; set; }
public int PercentComplete { get; set; }
}
}