WPF中如何获取ListBox中鼠标悬停位置的Index

zhangsongyi 2014-08-13 08:00:22
如题,目前采用的方案是监听ListBoxItem的MouseEnter路由事件,但无法成功

class MyListBox : ListBox
{
public MyListBox()
{
this.AddHandler(ListBoxItem.MouseEnterEvent, new RoutedEventHandler(OnMouseEnterItem));
}

void OnMouseEnterItem(object sender, RoutedEventArgs e)
{
//但在此处无论是sender还是e.OriginalSource类型都是MyListBox
}
}

请问是哪里出现了错误?或者有没有更好的解决方案?
...全文
610 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangsongyi 2014-08-13
  • 打赏
  • 举报
回复
引用 3 楼 Ptrtoptr 的回复:
等我们自己会搭框架的时候,就不会再有无数个这样让人烦恼,无聊的问题了。我为了挣点积分,不容易啊,希望你能看上 public Abb() { InitializeComponent(); List<string> s = new List<string>(); s.Add("a"); s.Add("b"); s.Add("c"); s.Add("d"); s.Add("e"); s.Add("f"); MyListBox box = new MyListBox(); box.ItemsSource = s; Style st = new System.Windows.Style(typeof(ListBoxItem),null); EventSetter e = new EventSetter(ListBoxItem.MouseEnterEvent, new MouseEventHandler(item_MouseEnter)); st.Setters.Add(e); box.ItemContainerStyle = st; Content = box; } void item_MouseEnter(object sender, MouseEventArgs e) { ListBoxItem i = sender as ListBoxItem; }
感谢,但我是用资源文件定义的ItemContainerStyle , 在添加Setter时会报错:”使用“SetterBaseCollection”之后(密封),无法对其进行修改。”
Ptrtoptr 2014-08-13
  • 打赏
  • 举报
回复
等我们自己会搭框架的时候,就不会再有无数个这样让人烦恼,无聊的问题了。我为了挣点积分,不容易啊,希望你能看上 public Abb() { InitializeComponent(); List<string> s = new List<string>(); s.Add("a"); s.Add("b"); s.Add("c"); s.Add("d"); s.Add("e"); s.Add("f"); MyListBox box = new MyListBox(); box.ItemsSource = s; Style st = new System.Windows.Style(typeof(ListBoxItem),null); EventSetter e = new EventSetter(ListBoxItem.MouseEnterEvent, new MouseEventHandler(item_MouseEnter)); st.Setters.Add(e); box.ItemContainerStyle = st; Content = box; } void item_MouseEnter(object sender, MouseEventArgs e) { ListBoxItem i = sender as ListBoxItem; }
zhangsongyi 2014-08-13
  • 打赏
  • 举报
回复
引用 1 楼 Ptrtoptr 的回复:
事件挂错地方了 挂在 this.AddHandler(ListBoxItem.MouseEnterEvent, ... 虽然表面上用的是ListBoxItem.MouseEnterEvent,实际上挂到了this(mylistbox)上, 用 ListBoxItem item = new ListBoxItem(); item.MouseEnter+=OnMouseEnterItem; myListBox.items.add(item);
我是用数据绑定的方式生成ListBox的 另外ListBox中的Items是数据项的集合而非ListBoxItem,没法订阅事件
Ptrtoptr 2014-08-13
  • 打赏
  • 举报
回复
事件挂错地方了 挂在 this.AddHandler(ListBoxItem.MouseEnterEvent, ... 虽然表面上用的是ListBoxItem.MouseEnterEvent,实际上挂到了this(mylistbox)上, 用 ListBoxItem item = new ListBoxItem(); item.MouseEnter+=OnMouseEnterItem; myListBox.items.add(item);
Ptrtoptr 2014-08-13
  • 打赏
  • 举报
回复
我这里测试没有发现异常,你再检查下 。实在不行,贴全代码吧,有闲人会来看 <local:MyListBox Name="b"> <local:MyListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <EventSetter Event="ListBoxItem.MouseEnter" Handler="item_MouseEnter"> </EventSetter> </Style> </local:MyListBox.ItemContainerStyle> </local:MyListBox>
  • 打赏
  • 举报
回复 1
ControlTemplate中的子控件 增加MouseEnter事件

<ListBox.ItemContainerStyle>
                        <Style TargetType="{x:Type ListBoxItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                        <StackPanel MouseEnter="StackPanel_MouseEnter">
                                            <TextBlock Width="20" Background="Red" Text="{Binding}"></TextBlock>
                                        </StackPanel>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ListBox.ItemContainerStyle>
                </ListBox>
后台:

StackPanel panel = e.OriginalSource as StackPanel;
            ListBoxItem parent = CommonHelper.GetParentObject<ListBoxItem>(panel) as ListBoxItem;
            int index = list.ItemContainerGenerator.IndexFromContainer(parent);  // 索引
GetParentObject方法,参见: http://blog.csdn.net/duanzi_peng/article/details/17094639

111,109

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧