Disable event bubbling on selectionchanged event- WPF

巧了我就是萌 提交于 2019-12-11 01:35:19

问题


I have a listbox inside a listview. on Both of them I have selectionchanged event. When I fire child control event parent control fires automatically. I need to stop this behaviour. Any hints? Thanks!

<ListView Name="listView1"
          ItemContainerStyle="{StaticResource RowStyle}" AlternationCount="2"
          SelectionChanged="listViewTask_SelectionChanged">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="150">
                <GridViewColumnHeader Name="gridViewColumnHeader1"
                                      Click="SortClick" Tag="Style"
                                      Content="Style"
                                      ToolTip="Click to Sort Style"/>
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <ListBox ScrollViewer.VerticalScrollBarVisibility="Hidden"
                             ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                             BorderBrush="White"
                             SelectionChanged="listBox_SelectionChanged" 
                             Name="listbox" ItemsSource="{Binding Text}"
                             Style="{StaticResource Textblockstyle}"
                             ToolTipService.ShowDuration="360000000">

--


回答1:


you can use the OriginalSource property in the selection changes event

if( ((ListView)e.OriginalSource).Name.Equals("name of the listview"))
  {
   // handle the event here...

  }



回答2:


In the inner handler set

e.Handled = true;


来源:https://stackoverflow.com/questions/7033691/disable-event-bubbling-on-selectionchanged-event-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!