从未调用WPF ListBoxItem事件

从未调用WPF ListBoxItem事件,wpf,events,listboxitem,Wpf,Events,Listboxitem,我想触发ListBoxItem上的事件 <ListBox.ItemTemplate> <DataTemplate> </DataTemplate> </ListBox.ItemTemplate> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxIt

我想触发ListBoxItem上的事件

        <ListBox.ItemTemplate>

            <DataTemplate>

            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">

               <EventSetter Event="Drop"     
                        Handler="Item_Drop"/>

                <EventSetter Event="PreviewMouseLeftButtonDown" 
                       Handler="Item_PreviewMouseLeftButtonDown"  />
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
从不调用事件处理程序,我做错了什么


顺便说一句,DataTemplate包含一个网格控件。我已经尝试使用网格控件上的事件。同样的结果是,事件处理程序从未被调用或访问。

仅供参考,我没有一个环境来检查这一点。我将描述这个问题及其解决方案。但我的密码可能有错误

主要是我不记得默认情况下边界或网格是否延伸。 测试这一点的主要方法是应用一种颜色,并尝试宽度+高度来检查您的事件

事件设定者很好。 这里的问题是没有检测到该事件。 这样做的原因是,您的项目的宽度为0,高度为0,因此它不会占用任何可单击或放置的空间

另一个原因是颜色。 如果拉伸的项目没有颜色,则不会绘制像素,因此即使在此时也不会发生事件。 即使应用Background=“Transparent”也可以做到这一点

         <ListBox.ItemTemplate>

            <DataTemplate>
                  <Border Background="Red" />  <!-- Or Grid or any thing that stretches out -->  
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">

               <EventSetter Event="Drop"     
                        Handler="Item_Drop"/>

                <EventSetter Event="PreviewMouseLeftButtonDown" 
                       Handler="Item_PreviewMouseLeftButtonDown"  />
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>

嘿,埃兰,非常感谢。在datatemplate中扩展网格是关键。调用现在可以正常工作了。
         <ListBox.ItemTemplate>

            <DataTemplate>
                  <Border Background="Red" />  <!-- Or Grid or any thing that stretches out -->  
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">

               <EventSetter Event="Drop"     
                        Handler="Item_Drop"/>

                <EventSetter Event="PreviewMouseLeftButtonDown" 
                       Handler="Item_PreviewMouseLeftButtonDown"  />
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>