none
wpf listbox点击item时事件 RRS feed

  • 问题

  • 需要点击listbox中item的事件触发出来,但是使用selectedchanged不能全部实现,因当我点击的是选中item时

    望解答

    ths

    • 已移动 Jamles Hez 2013年11月12日 1:23 wpf related
    2013年11月11日 2:02

全部回复

  • Hi Skczjun,

    WPF?不知道您是否发错了论坛,如果是的话,我可以帮你移到WPF论坛中去。

    我想了下在WPF中你可以尝试如下方法:

    新建一个继承ListBoxItem的类比如说ClickableListBoxItem,定义上Click事件,同样为了使用ClickableListBoxItem,一个继承ListBox的类也是需要的。在使用的时候给ClickableListBoxItem写上对应的Click处理代码即可。

    或者还有一种更简便的方法如下:

    <ListBox>
        ....
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                <EventSetter Event="MouseClick" Handler="ListBoxItem_MouseClick"/>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    2013年11月11日 3:11
  • 谢谢 请帮我移到wpf论坛
    2013年11月12日 1:14
  • Hi Skczjun,

    WPF?不知道您是否发错了论坛,如果是的话,我可以帮你移到WPF论坛中去。

    我想了下在WPF中你可以尝试如下方法:

    新建一个继承ListBoxItem的类比如说ClickableListBoxItem,定义上Click事件,同样为了使用ClickableListBoxItem,一个继承ListBox的类也是需要的。在使用的时候给ClickableListBoxItem写上对应的Click处理代码即可。

    或者还有一种更简便的方法如下:

    <ListBox>
        ....
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                <EventSetter Event="MouseClick" Handler="ListBoxItem_MouseClick"/>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    使用

    <ListBox> .... <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"> <EventSetter Event="MouseClick" Handler="ListBoxItem_MouseClick"/> </Style> </ListBox.ItemContainerStyle> </ListBox>

    后,点击item没有收到ListBoxItem_MouseClick事件处理,事件是否被截取了?


    2013年11月12日 1:21
  • 可能在EventSetter中的Event不应该是MouseClick,貌似MouseClick事件不是WPF的,WPF对应的是Click,你再试试看呢。

    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年11月12日 1:37
  • 可能在EventSetter中的Event不应该是MouseClick,貌似MouseClick事件不是WPF的,WPF对应的是Click,你再试试看呢。

    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    我没有使用MouseClick,因为wpf没有这个,我使用的是MouseDown,没有触发

    <Setter Property="ItemContainerStyle">
                    <Setter.Value>
                        <Style TargetType="ListBoxItem">
                            <EventSetter Event="MouseDown" Handler="ListBoxItem_MouseClick"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ListBoxItem">
                                        <Border>
                                            <ContentPresenter/>
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </Setter.Value>
                </Setter>
    • 已编辑 skczjun 2013年11月18日 8:30
    2013年11月12日 6:50