使用菜单快捷方式保存时未拾取 Propertygrid 更改
我正在尝试实现一个简单的 Windows 窗体应用程序,用户可以在其中编辑简单的 Person 类的实例。该应用程序利用标准 propertygrid,将 Person 类的实例分配给 propertygrid 的 SelectedObject 属性。当用户单击菜单项“保存”时,应用程序从属性网格中获取所选项目并保存它。这很好用。
但是,如果使用分配给菜单项的快捷键(例如 ctrl+S)来保存属性网格的 SelectedObject,则不会拾取更改。这似乎与以下事实有关:除非 propertygrid 中的字段失去焦点,否则 Person 对象的属性不会更新,并且 ctrl+S 快捷键不会从 propertygrid 中的字段夺走焦点。
其他开发人员(例如 Mark Gilbert)已经通过将焦点从属性网格上移开来解决了这个问题,但这似乎有点麻烦,特别是因为将焦点保持在现场会很好。 Visual Studio 开发人员显然是正确的,但我还没弄清楚如何做。
I am trying to implement a simple windows forms application, where the user can edit instances of a simple Person class. The application makes use of the standard propertygrid, assigning instances of the Person class to the SelectedObject property of the propertygrid. When the user clicks the menu item Save, the application gets the selected item from the propertygrid and saves it. This works fine.
However, if a shortcut such as ctrl+S, assigned to the menu item, is used for saving the SelectedObject of the propertygrid, then the changes are not picked up. It appears to be related to the fact that the properties of the Person object are not updated unless the field in the propertygrid looses focus, and the ctrl+S shortcut doesn’t take away focus from fields in the propertygrid.
Other developers, such as Mark Gilbert, have solved this issue by forcing away focus from the propertygrid, but this seems to be a bit of a hack, especially since it would be nice to keep the focus at the field. The Visual Studio developers apparently got it right, but I haven’t figured out how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有两种不同的方法:
正如 Mark Gilbert 和 @Tergiver 所解释的,您可以从就地控件中删除焦点,该控件将在触发菜单命令之前提交值更改。由于您必须从 OnSave 处理程序中执行此操作,因此您需要从那里对网格进行依赖。
你也可以反其道而行之。从 PropertyGrid 派生并覆盖 ProcessCmdKey。如果检测到 Ctrl+S,请将焦点更改为网格本身,然后调用 base.ProcessCmdKey()。与第 1 点相反,这依赖于网格中应用程序的快捷键(至少 Ctrl+S)。我应该补充一点,我没有尝试过这种方法,但理论上它应该有效。
你的选择...
You have 2 different approaches:
As Mark Gilbert and @Tergiver explain, you can remove the focus from the inplace control which will commit the value change before the menu command is triggered. Since you have to do this from your OnSave handler, you put a dependency on the grid from there.
You can also do the contrary. Derive from the PropertyGrid and override the ProcessCmdKey. If you detect a Ctrl+S, change the focus to the grid itself then call base.ProcessCmdKey(). Contrarily to point 1, this puts a dependency on the shortcut keys of the application (Ctrl+S at least) from the grid. I should add that I didn't try this method but in theory it should work.
Your choice...
对 PropertyGrid 本身调用 Focus。这将结束编辑模式并更新选定的对象,但选定的属性不会更改。
Call Focus on the PropertyGrid itself. This ends edit mode and updates the selected object(s), but the selected property doesn't change.