如何为 ListBoxItems 创建动态上下文菜单?
How do I create a dynamic context menu for ListBoxItems?
我的风格似乎行不通。尽管 Snoop 告诉我 ListBoxItem
的 DataContext
是正确的,但什么也没有显示。如果 Commands
的绑定有问题,我希望看到一个空的上下文菜单出现。
风格:
<ContextMenu x:Key="CommandsContextMenu" ItemsSource="{Binding Commands}">
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding Name}"/>
</Style>
</ContextMenu>
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu" Value="{StaticResource CommandsContextMenu}" />
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<Binding Path="DataContext.HasCommands" />
</DataTrigger.Binding>
</DataTrigger>
</Style.Triggers>
</Style>
探听DataContext:
显示上下文菜单 属性 的探听属性甚至都没有设置。
这里的想法是,在不知道任何类型的情况下,我可以有一个列表框项目样式,如果它绑定到的东西有一个名为 HasCommands 的 属性,并且它被设置为 true,然后它将在该列表框项目上设置一个上下文菜单,绑定到命令 属性.
我没有收到来自 PresentationTraceSources.DataBindingSource
的任何绑定错误或警告
为什么没有设置上下文菜单?
事实证明,问题出在使用继承自 ListBox
的东西
参考这里是我定义的 class:
// adapted from
// which was probably adapted from http://wpftutorial.net/DragAndDrop.html
type DragDropListBox() as self =
inherit ListBox()
从那里只挂钩以下
- self.PreviewMouseLeftButtonUp
- self.PreviewMouseMove
- 覆盖x.OnItemsSourceChanged
并且在初始化中它覆盖了 ItemContainerStyle 如下:
事实证明,问题出在使用继承自 ListBox
的东西
供参考,这是我的 class:
的顶部
// adapted from
// which was probably adapted from http://wpftutorial.net/DragAndDrop.html
type DragDropListBox() as self =
inherit ListBox()
从那里只挂钩以下
- self.PreviewMouseLeftButtonUp
- self.PreviewMouseMove
- 覆盖x.OnItemsSourceChanged
并且在初始化中它覆盖了 ItemContainerStyle 如下:
do
self.PreviewMouseLeftButtonUp.Add listBoxPreviewMouseLeftButtonUp
//self.PreviewMouseLeftButtonDown.Add listBoxPreviewMouseMove //.AddHandler (MouseButtonEventHandler previewMouseMove)
self.PreviewMouseMove.Add listBoxPreviewMouseMove
let style =
let s = Style(typeof<ListBoxItem>)
s.Setters.Add (Setter(ListBoxItem.AllowDropProperty, true))
s.Setters.Add (EventSetter(ListBoxItem.PreviewMouseLeftButtonDownEvent, MouseButtonEventHandler listBoxPreviewMouseLeftButtonDown))
s.Setters.Add (EventSetter(ListBoxItem.DropEvent, DragEventHandler listBoxItemDrop))
s
self.ItemContainerStyle <- style
现在我要弄清楚是否可以将两种样式加在一起。
我的风格似乎行不通。尽管 Snoop 告诉我 ListBoxItem
的 DataContext
是正确的,但什么也没有显示。如果 Commands
的绑定有问题,我希望看到一个空的上下文菜单出现。
风格:
<ContextMenu x:Key="CommandsContextMenu" ItemsSource="{Binding Commands}">
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding Name}"/>
</Style>
</ContextMenu>
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu" Value="{StaticResource CommandsContextMenu}" />
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<Binding Path="DataContext.HasCommands" />
</DataTrigger.Binding>
</DataTrigger>
</Style.Triggers>
</Style>
探听DataContext:
显示上下文菜单 属性 的探听属性甚至都没有设置。
这里的想法是,在不知道任何类型的情况下,我可以有一个列表框项目样式,如果它绑定到的东西有一个名为 HasCommands 的 属性,并且它被设置为 true,然后它将在该列表框项目上设置一个上下文菜单,绑定到命令 属性.
我没有收到来自 PresentationTraceSources.DataBindingSource
为什么没有设置上下文菜单?
事实证明,问题出在使用继承自 ListBox
参考这里是我定义的 class:
// adapted from
// which was probably adapted from http://wpftutorial.net/DragAndDrop.html
type DragDropListBox() as self =
inherit ListBox()
从那里只挂钩以下
- self.PreviewMouseLeftButtonUp
- self.PreviewMouseMove
- 覆盖x.OnItemsSourceChanged
并且在初始化中它覆盖了 ItemContainerStyle 如下:
事实证明,问题出在使用继承自 ListBox
供参考,这是我的 class:
的顶部// adapted from
// which was probably adapted from http://wpftutorial.net/DragAndDrop.html
type DragDropListBox() as self =
inherit ListBox()
从那里只挂钩以下
- self.PreviewMouseLeftButtonUp
- self.PreviewMouseMove
- 覆盖x.OnItemsSourceChanged
并且在初始化中它覆盖了 ItemContainerStyle 如下:
do
self.PreviewMouseLeftButtonUp.Add listBoxPreviewMouseLeftButtonUp
//self.PreviewMouseLeftButtonDown.Add listBoxPreviewMouseMove //.AddHandler (MouseButtonEventHandler previewMouseMove)
self.PreviewMouseMove.Add listBoxPreviewMouseMove
let style =
let s = Style(typeof<ListBoxItem>)
s.Setters.Add (Setter(ListBoxItem.AllowDropProperty, true))
s.Setters.Add (EventSetter(ListBoxItem.PreviewMouseLeftButtonDownEvent, MouseButtonEventHandler listBoxPreviewMouseLeftButtonDown))
s.Setters.Add (EventSetter(ListBoxItem.DropEvent, DragEventHandler listBoxItemDrop))
s
self.ItemContainerStyle <- style
现在我要弄清楚是否可以将两种样式加在一起。