创建一个自定义控件,该控件是一个也具有 Items 和 ItemsSource 的 Button?

发布于 2024-07-24 01:57:23 字数 889 浏览 0 评论 0原文

我正在尝试创建一个使用 ContextMenu 来显示其项目的 SplitButton 控件。 SplitButton 当前派生自 Button。 我有一个 PART_DropDownInitiator ,它是用户单击以调出 ContextMenu 以便 SplitButton 显示其项目的小图标。

问题是我试图在 SplitButtonTemplate 或样式内设置默认的 ContextMenu ,这样用户就可以使用控制不必自己做。 起初我想我只需让用户在 xaml 中设置 SplitButton.ContextMenu.Items 属性即可,但这似乎不可能。 现在我想将 ItemsItemsSource 属性添加到 SplitButton 中,ContextMenu 中的 >SplitButton 的模板可以绑定到它。 用户应该不仅仅需要在 xaml 中或 SplitButton 上的代码隐藏中设置 ItemsItemsSource 属性...

但是,两者ItemsItemsSource 属性似乎源自 ItemsControl,这是一个问题,因为 SplitButton 已经派生自按钮。 所以我不知道该怎么做...有什么想法吗?

I'm trying to create a SplitButton control that uses a ContextMenu to display its items. The SplitButton is currently deriving from Button. I have a PART_DropDownInitiator that is the little icon the user clicks on to bring up the ContextMenu for the SplitButton to show its items.

The problem is that I'm trying to setup a default ContextMenu inside of the SplitButton's Template or style, this way the user of the control doesn't have to do it themselves. At first I figured I'd just have the user just set the SplitButton.ContextMenu.Items property in xaml, but that doesn't appear to be possible. Now I'd like to add an Items and ItemsSource properties to the SplitButton that the ContextMenu within the SplitButton's template can bind to it. And the user should than just have to set the Items or ItemsSource property in xaml or code-behind on the SplitButton...

But, both the Items and ItemsSource properties seem to originate from ItemsControl, which is a problem because the SplitButton is already deriving from Button. So I'm not sure what to do... Any ideas?

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

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

发布评论

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

评论(1

舟遥客 2024-07-31 01:57:23

如果您要采用继承路线,则应该从 HeaderedItemsControl 派生,其中默认 ControlTemplate 将 Header 内容作为 Button 的内容。

结果,您还获得了 Items/ItemsSource——您唯一需要开发的是您的 ControlTemplate。

最后,考虑将菜单部分实现为弹出窗口而不是上下文菜单。 ContextMenu 是特定用途的 UI 元素(通常通过右键单击调出)。 有关详细信息,请参阅 aero.normalcolor.xaml 中 ComboBox 的默认 ControlTemplate。

If you're going the inheritance route, you should derive from HeaderedItemsControl instead, where your default ControlTemplate puts the Header content as the contents of a Button.

As a result, you also get Items/ItemsSource -- the only thing you have to develop is your ControlTemplate.

Lastly, consider implementing the menu portion as a Popup instead of a ContextMenu. ContextMenu's are a purpose-specific UI element (and generally brought up with a right-click). For more information, see the default ControlTemplate of ComboBox in aero.normalcolor.xaml.

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