在 WPF 中重用按钮

发布于 2024-08-27 07:03:26 字数 195 浏览 4 评论 0原文

我有一堆不同的对象,通常使用不同的 DataTemplate 在同一个 TabControl 中编辑,但我希望每个 DataTemplate 具有共同的外观和感觉,每个选项卡右下角的“确定”和“取消”按钮将关闭选项卡或保存内容,然后关闭当前选定的选项卡。在每个选项卡上放置按钮的最佳方式是什么?有没有一种方法可以做到这一点,而无需在我的所有数据模板中复制和粘贴按钮和堆栈面板?

I have a bunch of different objects that are commonly edited in the same TabControl using different DataTemplates, but I want each DataTemplate to have a common look and feel with Ok and Cancel buttons at the bottom right of each tab that will close the tab or save the content and then close the currently selected tab. What's the best way to place buttons on each tab ? Is there a way to do it without copying and pasting the buttons and stack panel across all of my data templates ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

寒江雪… 2024-09-03 07:03:26

当然,您可以创建自己的 OkCancelSaveControl。在 WPF 中,创建“用户控件”比听起来容易得多。 这是一个教程。简而言之,您

  1. 创建一个新的用户控件,
  2. 在用户控件中创建属性,为您的控件提供执行其职责所需的信息(例如,它应该关闭的选项卡或它应该保存的数据对象),
  3. 如有必要,创建用户控件引发的事件 (OkClick),以防某些选项卡需要特殊处理。

Sure, you can create your own OkCancelSaveControl. In WPF, creating a "user control" is much easier than it sounds. Here is a tutorial. In a nutshell, you

  1. create a new user control,
  2. create properties in the user control that give the your control the information it needs to perform its duties (e.g. the tab that it's supposed to close or the data object that it's supposed to save),
  3. if necessary, create events that the user control raises (OkClick), in case some tab requires special treatment.
初心 2024-09-03 07:03:26

我将制作一个自定义控件,我们将其称为 MyCoolTabItem,它继承自 TabItem 类,然后将按钮放入该控件中。然后只需将 MyCoolTabItem 而不是 TabItem 添加到所有 TabControls 中,它就会包含所有按钮。

I would make a custom control, lets call it MyCoolTabItem, that inherits from the TabItem class, and just throw your buttons into the control. Then just add a MyCoolTabItem instead of a TabItem to all of your TabControls and it will have all of your buttons on it.

素年丶 2024-09-03 07:03:26

您可以创建一个包含这些按钮的基本视图类。需要按钮的视图将继承它们和通用功能。

You could make a base view class that held those buttons. Views that needed the buttons would inherit them and common functionality.

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