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;