DevExpress 表格再新增行后滚动条自动移动到新增行

DevExpress 表格再新增行后滚动条自动移动到新增行

dxg:GridControl

dxg:TableView

WPF项目

Visual Studio

1. 前端代码

前端有两种方法,一种是GridControl的CurrentItem属性,一种是TableView的FocusedRow属性,二选一即可

示例:

xml 复制代码
<dxg:GridControl CurrentItem="{Binding CurrentLogInfo,Mode=TwoWay}">
                <dxg:GridControl.View>
                    <dxg:TableView AllowEditing="False" FocusedRow="{Binding CurrentLogInfo,Mode=TwoWay}"/>
                </dxg:GridControl.View>
                <dxg:GridControl.Columns>
                </dxg:GridControl.Columns>
            </dxg:GridControl>

2. 后端代码

继承INotifyPropertyChanged并实现RaisePropertyChanged

csharp 复制代码
public class LogViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<LogInfo> logList { get; set; }
        private object _syncLock = new Object();
        public ICollectionView CollectionView { get; set; }

        public LogInfo CurrentLogInfo
        {
            get
            {
                return _currentLogInfo;
            }

            set
            {
                _currentLogInfo = value;
                RaisePropertyChanged("CurrentLogInfo");
            }
        }
        LogInfo _currentLogInfo;
        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public LogViewModel()
        {
            logList = new ObservableCollection<LogInfo>();

            CollectionView = CollectionViewSource.GetDefaultView(logList);
            BindingOperations.EnableCollectionSynchronization(logList, _syncLock);
        }
    }

这样我们在改变CurrentLogInfo时,就会通知客户端属性值已更改。

csharp 复制代码
logvm.logList.Add(log);
logvm.CurrentLogInfo = log;
相关推荐
CoderIsArt10 小时前
C#中UI 线程与 Dispatcher
开发语言·ui·c#
呆呆敲代码的小Y1 天前
【游戏开发】Lua 与 C# 交互原理解析
c#·lua·交互·热更新·lua与c#交互·游戏热更新
用户298698530141 天前
PDF 转纯文本(TXT)免费攻略:轻松提取文字内容
人工智能·后端·c#
呆呆敲代码的小Y1 天前
【游戏开发】C# 中的迭代器
java·开发语言·c#·迭代器
2601_962174172 天前
Redis 简介
数据库·redis·wpf
格林威2 天前
C# 相机Burst模式图像采集:使用相机内存配合OpenCvSharp和Halcon实现短时间的高速采集的方法
开发语言·人工智能·数码相机·计算机视觉·c#·视觉检测·工业相机
J总裁的小芒果2 天前
参数过多-分批传参
c#
典典分享指南2 天前
短视频分镜提示词:让 AI 出片的产品口播
java·c#·bash·composer·symfony
Z5998178412 天前
c#软件开发学习笔记--WPF(Canvas、数据绑定、MVVM模式、值转换器)
笔记·学习·c#