PreviewMouseLeftButtonDown没有触发

时间:2010-06-23 01:42:41

标签: c# wpf events

我有一个ListBox,一个IEnumerable是数据源。单击ListBoxItem时,我想访问该对象,以便我可以获取一些数据并显示另一个窗口。

这是我的ListBox XAML

          `<ListBox Name="listBox1" Margin="0" Width="1010" Height="275" BorderThickness="0" BorderBrush="{x:Null}" Cursor="Arrow" HorizontalAlignment="Center" VerticalAlignment="Top" SelectionMode="Single" FontFamily="DIN" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Focusable="False" IsHitTestVisible="False" IsTextSearchEnabled="False" >`

            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListBox_MouseLeftButtonDown"></EventSetter>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate DataType="{x:Type local:Offer}">
                    <StackPanel Margin="0" Width="200" Height="275" Background="Black" Name="sp">
                        <Image Source="{Binding Image}" Width="200" Height="131" Margin="0"></Image>
                        <TextBlock Padding="5" Background="Black" Text="{Binding Name}" Foreground="White" FontFamily="DIN medium" FontWeight="Bold"  FontSize="16" Width="200" Margin="0"></TextBlock>
                        <TextBlock Padding="5,0,5,0" Background="Black" Text="{Binding Date}" Foreground="White" FontFamily="DIN medium" FontWeight="Bold" FontSize="14" Width="200" Margin="0"></TextBlock>
                        <TextBlock Padding="5" Background="Black" Text="{Binding Description}" Foreground="White" FontFamily="DIN light" FontSize="16" Width="200" Margin="0"  TextWrapping="WrapWithOverflow"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Background="Black" CanHorizontallyScroll="True" CanVerticallyScroll="False" FlowDirection="LeftToRight" Margin="0" Orientation="Horizontal" Width="1010" Height="275"></VirtualizingStackPanel>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>`$

其他相关信息

                CurrentItems = (from offerCatType in offerRes.OfferCategory
                                       where offerCatType.type == Type
                                       from offers in offerCatType.Offer
                                       where new       DateTime(Convert.ToDateTime(offers.startDate).Year,
  Convert.ToDateTime(offers.startDate).Month, 1)  <= MonthYear && Convert.ToDateTime(offers.endDate) >= MonthYear
                                       select new Offer
                                       {
                                           Name = offers.name,
                                           Description = offers.description,
                                           Date = String.Format("{0:dd/MM/yyyy}",       Convert.ToDateTime(offers.startDate)) + " to " + String.Format("{0:dd/MM/yyyy}",   Convert.ToDateTime(offers.endDate)),
                                           ClickThruUrl = offers.ChannelInfo.refClickThroughLink,
                                           ReferenceID = offers.ChannelInfo.refId,
                                           Image = offers.ChannelInfo.refLink
                                       }
                        );



        listBox1.ItemsSource = CurrentItems;
        protected void ListBox_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {}

我的一些造型有可能吹走这个事件吗?我今天早些时候工作,然后修复了几个样式项,然后,点击代码停止工作。

1 个答案:

答案 0 :(得分:0)

将列表框的IsHitTestVisible属性设置为true而不是false,您将获得鼠标事件。