WPF:DataGrid 查找和替换
我使用 DataGrid 来显示对象集合的特定属性的值。我通过与 Tomer Shamam 的博客。
但是,我现在需要实现“查找/替换”类型的功能。我以为我能够迭代 DataGrid 的单元格来执行突出显示和替换,但似乎没有一种简单的方法可以做到这一点。
有什么想法吗?
I'm using a DataGrid to display the values of specific properties of a collection of objects. I've implemented search and cell highlighting through a similar method to the one on Tomer Shamam's blog.
However, I now need to implement 'Find/Replace' type functionality. I presumed I would be able to iterate through the DataGrid's cells to perform highlighting and replacements, but there doesn't seem to be a simple way to do this.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为在这种情况下你可能有一个错误的心态,也许你是从 WinForms 来到 WPF 世界......
在 WPF DataGrid 中你基本上从不通过 DataGrid 操作数据,你总是直接在 DataSource 上工作。至于“似乎没有一个简单的方法可以做到这一点” - 你是对的。这将会比应有的更加困难。
如果我要实现查找/替换功能 - 每次单击都会首先突出显示下一个出现的情况,然后我可以选择跳过/查找下一个或替换 - 那么我会这样做:
1)我们需要知道我们当前的位置 -
DataGrid.CurrentItem
给出当前(数据源)对象;2) 现在,我们对数据源执行搜索,以从当前对象位置开始查找下一个出现的位置(例如
var indx = List
I think you perhaps have a wrong mindset in this scenario, perhaps you are coming from WinForms to the WPF world...
In WPF DataGrid you basically never manipulate data via the DataGrid, you always work on the DataSource directly. As to "there doesn't seem to be a simple way to do this" - you are right. It's going to be more difficult than it should.
If I was to implement a Find/Replace feature - each click would first highlight the next occurence then I can either choose to skip/find_next or replace - then this is how I would do it:
1) We need to know our current position -
DataGrid.CurrentItem
gives the current (data source) object;2) Now we perform a search on the data source to find the next occurence starting from the current objects location (eg.
var indx = List<object>.FindIndex(...)
followed byvar nextItem = List<object>[indx]
);3) Then we need to scroll the DataGrid to the found object and bring the DataGridRow into view -
DataGrid.ScrollIntoView(nextItem);
(you might need to do DataGrid.UpdateLayout() before the call, there appears to be some quirks with the .NET 4 built-in DataGrid in my experience);4) You should already know how to highlight a cell...;
5) Wait for user input, either skip or replace;
6) If we replace then we can either use DataGrid.CurrentItem or the nextItem variable and replace some value with new value. Depending on how you have set up your DataGrid, you might need to do some Refresh()/UpdateLayout() call or a BindingOperations.GetBindingExpression(...).UpdateTarget() call to update the DataGrid;
7) Finally go back to step 1 and repeat;