使用菜单快捷方式保存时未拾取 Propertygrid 更改

发布于 2024-10-16 16:30:48 字数 456 浏览 8 评论 0原文

我正在尝试实现一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放赐 2024-10-23 16:30:48

您有两种不同的方法:

  1. 正如 Mark Gilbert 和 @Tergiver 所解释的,您可以从就地控件中删除焦点,该控件将在触发菜单命令之前提交值更改。由于您必须从 OnSave 处理程序中执行此操作,因此您需要从那里对网格进行依赖。

  2. 你也可以反其道而行之。从 PropertyGrid 派生并覆盖 ProcessCmdKey。如果检测到 Ctrl+S,请将焦点更改为网格本身,然后调用 base.ProcessCmdKey()。与第 1 点相反,这依赖于网格中应用程序的快捷键(至少 Ctrl+S)。我应该补充一点,我没有尝试过这种方法,但理论上它应该有效。

你的选择...

You have 2 different approaches:

  1. 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.

  2. 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...

帥小哥 2024-10-23 16:30:48

对 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文