使用进度条创建自定义单元格元素时,RadListView具有奇怪的行为

时间:2014-08-07 16:04:32

标签: winforms telerik

当我需要在listview的单元格中创建进度条时,我遇到了RadListView for WinForms的一个非常奇怪的行为。它最初创建带有进度条的单元格但是当我向左和向右滚动列表视图时,进度条元素移动到左侧一列。

以下是我使用进度条插入创建单元格的代码:

_processingList = new RadListView();
_processingList.CellCreating += new ListViewCellElementCreatingEventHandler(_list_CellCreating);
_processingList.ViewType = ListViewType.DetailsView;
_processingList.Dock = System.Windows.Forms.DockStyle.Fill;
_processingList.HeaderHeight = 16;
_processingList.Columns.Add("Source");
_processingList.Columns.Add("Dir");
_processingList.Columns.Add("Destination");
_processingListProgressColumn = new ListViewDetailColumn("Progress", "");
_processingList.Columns.Add(_processingListProgressColumn);
_processingList.Columns.Add("Size");
_processingList.Columns.Add("Priority");
_processingList.Columns.Add("Status");
_processingList.Columns.Add("Start Time");
_processingList.Columns.Add("Elapsed");
_processingList.Columns.Add("Left");

...

void _list_CellCreating(object sender, ListViewCellElementCreatingEventArgs e)
{
    DetailListViewDataCellElement dataCell = e.CellElement as DetailListViewDataCellElement;

    if (dataCell != null && dataCell.Data == _processingListProgressColumn)
    {
        e.CellElement = new ProgressBarCellElement(dataCell.RowElement, dataCell.Data);
    }
}

class ProgressBarCellElement : DetailListViewDataCellElement
{
    private RadProgressBarElement progressBar;

    public ProgressBarCellElement(DetailListViewVisualItem owner, ListViewDetailColumn column)
        : base(owner, column)
    {

    }

    protected override void CreateChildElements()
    {
        base.CreateChildElements();

        this.progressBar = new RadProgressBarElement();
        this.progressBar.StretchHorizontally = this.progressBar.StretchVertically = true;

        this.Children.Add(this.progressBar);
    }

    public override void Synchronize()
    {

        base.Synchronize();
        this.Text = "";

        try
        {
            this.progressBar.Value1 = Convert.ToInt32(this.Row[this.column]);
        }
        catch { }

    }

    protected override Type ThemeEffectiveType
    {
        get
        {
            return base.ThemeEffectiveType;
        }
    }
}

有人可以有一个很好的解释或指出如何以正确的方式在某些单元格内创建带有ProgressBar的RadListView?

1 个答案:

答案 0 :(得分:0)

RadListView在详细信息视图中为其单元格使用虚拟化,因此在滚动等操作期间会重复使用单元格。要处理这种情况,您应该首先指定单元格应该使用哪个列,并且还应该为其余情况设置默认单元格。这是一个完整的例子:

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        InitializeComponent();

        radListView1 = new RadListView();
        radListView1.Parent = this;
        this.Controls.SetChildIndex(radListView1, 0);
        radListView1.CellCreating += new ListViewCellElementCreatingEventHandler(_list_CellCreating);
        radListView1.ViewType = ListViewType.DetailsView;
        radListView1.Dock = System.Windows.Forms.DockStyle.Fill;
        radListView1.HeaderHeight = 16;
        radListView1.Columns.Add("Source");
        radListView1.Columns.Add("Dir");
        radListView1.Columns.Add("Destination");
        _processingListProgressColumn = new ListViewDetailColumn("Progress", "");
        radListView1.Columns.Add(_processingListProgressColumn);
        radListView1.Columns.Add("Size");
        radListView1.Columns.Add("Priority");
        radListView1.Columns.Add("Status");
        radListView1.Columns.Add("Start Time");
        radListView1.Columns.Add("Elapsed");
        radListView1.Columns.Add("Left");

        for (int i = 0; i < 60; i++)
        {
            radListView1.Items.Add("src", "dir", "dest", i * 1.14, "123", "high", "stat", "start", "elapsed", "left");
        }
    }

    ListViewDetailColumn _processingListProgressColumn;

    private void _list_CellCreating(object sender, ListViewCellElementCreatingEventArgs e)
    {
        DetailListViewDataCellElement dataCell = e.CellElement as DetailListViewDataCellElement;

        if (dataCell != null)
        {
            if (dataCell.Data == _processingListProgressColumn)
            {
                e.CellElement = new ProgressBarCellElement(dataCell.RowElement, dataCell.Data);
            }
            else
            {
                e.CellElement = new DefaultCellElement(dataCell.RowElement, dataCell.Data);
            }
        }
    }

    class DefaultCellElement : DetailListViewDataCellElement
    {
        public DefaultCellElement(DetailListViewVisualItem owner, ListViewDetailColumn column) :
            base(owner, column)
        {

        }
        public override bool IsCompatible(ListViewDetailColumn data, object context)
        {
            return data.Name != "Progress";
        }

        protected override Type ThemeEffectiveType
        {
            get
            {
                return base.ThemeEffectiveType;
            }
        }
    }

    class ProgressBarCellElement : DetailListViewDataCellElement
    {
        private RadProgressBarElement progressBar;

        public ProgressBarCellElement(DetailListViewVisualItem owner, ListViewDetailColumn column)
            : base(owner, column)
        {

        }

        public override bool IsCompatible(ListViewDetailColumn data, object context)
        {
            return data.Name == "Progress";
        }

        protected override void CreateChildElements()
        {
            base.CreateChildElements();

            this.progressBar = new RadProgressBarElement();
            this.progressBar.StretchHorizontally = this.progressBar.StretchVertically = true;

            this.Children.Add(this.progressBar);
        }

        public override void Synchronize()
        {

            base.Synchronize();
            this.Text = "";

            try
            {
                this.progressBar.Value1 = Convert.ToInt32(this.Row[this.column]);
            }
            catch { }

        }

        protected override Type ThemeEffectiveType
        {
            get
            {
                return base.ThemeEffectiveType;
            }
        }
    }