创建一个自定义控件,该控件是一个也具有 Items 和 ItemsSource 的 Button?
我正在尝试创建一个使用 ContextMenu
来显示其项目的 SplitButton
控件。 SplitButton
当前派生自 Button
。 我有一个 PART_DropDownInitiator
,它是用户单击以调出 ContextMenu
以便 SplitButton
显示其项目的小图标。
问题是我试图在 SplitButton
的 Template
或样式内设置默认的 ContextMenu
,这样用户就可以使用控制不必自己做。 起初我想我只需让用户在 xaml 中设置 SplitButton.ContextMenu.Items
属性即可,但这似乎不可能。 现在我想将 Items
和 ItemsSource
属性添加到 SplitButton
中,ContextMenu
中的 >SplitButton
的模板可以绑定到它。 用户应该不仅仅需要在 xaml 中或 SplitButton
上的代码隐藏中设置 Items
或 ItemsSource
属性...
但是,两者Items
和 ItemsSource
属性似乎源自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要采用继承路线,则应该从 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.