如何为分段进度条创建适配DevExpress GridControl的RepositoryItem
DevExpress GridControl 嵌入自定义
SegmentedProgressBar实现步骤 - 前置适配:遵循DevExpress Grid的自定义编辑器开发规范,完成控件的注册封装:
- 基于现有
SegmentedProgressBar类,定义配套的RepositoryItem仓库项类,将自定义控件注册到DevExpress全局编辑器列表,暴露Bars数组、ContentLength两个可配置属性,支持在Grid设计器中直接绑定 - 定义配套的编辑器实体类,继承自
SegmentedProgressBar,实现和仓库项的属性自动同步逻辑 - 在目标Grid列的
ColumnEdit属性中,指定使用自定义的仓库项实例 - 挂载GridView的
CustomRowCellEdit事件,逐行从绑定数据源中读取当前行对应的下载分段数组、远程文件总大小,赋值给当前行的编辑器实例 - 依托现有
Bar类实现的INotifyPropertyChanged接口,在多线程下载实时更新分段长度时,自动触发控件重绘,无需手动调用刷新接口
- 基于现有
- 配套核心参考代码:
// 自定义进度条对应的仓库项 public class RepositoryItemSegmentedProgressBar : RepositoryItem { private const string EditorRegisterName = "SegmentedProgressBar"; static RepositoryItemSegmentedProgressBar() { // 注册到DevExpress全局编辑器池 EditorRegistrationInfo.Default.Editors.Add( new EditorClassInfo( EditorRegisterName, typeof(SegmentedProgressBarEdit), typeof(RepositoryItemSegmentedProgressBar), typeof(BaseEditViewInfo), new BaseEditPainter(), true, null ) ); } public override string EditorName => EditorRegisterName; // 暴露自定义进度条所需的两个核心参数 public Bar[] Bars { get; set; } public long ContentLength { get; set; } } // 实际承载绘制逻辑的编辑器类 public class SegmentedProgressBarEdit : SegmentedProgressBar { public override RepositoryItem OwnerEdit { get => base.OwnerEdit; set { base.OwnerEdit = value; // 从仓库项同步参数值 if (value is RepositoryItemSegmentedProgressBar repo) { this.Bars = repo.Bars; this.ContentLength = repo.ContentLength; } } } } // 逐行绑定数据的事件示例 private void gridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e) { if (e.Column.FieldName != "DownloadProgress") return; if (e.RepositoryItem is not RepositoryItemSegmentedProgressBar progressRepo) return; // 从当前行数据源取下载任务的对应参数 var downloadTask = e.Row as DownloadTaskModel; progressRepo.Bars = downloadTask.DownloadSegmentBars; progressRepo.ContentLength = downloadTask.TotalRemoteFileSize; }
现有代码修正提示:
Bar类的Start属性set块存在赋值逻辑错误,当前代码在设置起始偏移量时,错误将传入值赋值给了length字段,需修正为赋值给start字段,否则会导致分段进度条的位置计算完全错乱。
内容的提问来源于stack exchange,提问作者Ali Tor
相关产品推荐
相关产品推荐

