WPF Listview SelectionChanged event

前端 未结 5 1531
滥情空心
滥情空心 2021-02-05 17:04

I have a ListView binding to an ItemsSource and the SelectionChanged event is firing on the load/databound events? I assume that it is bec

5条回答
  •  鱼传尺愫
    2021-02-05 17:13

    The listView should not fire the SelectionChange if you only set the ItemsSource property. However if you bind the SelectedIndex property to a property of your dataContext object the selection will move to the index that is specified by the binded property.

    this doesn't fires the Selector_OnSelectionChanged event when the page loads:

    
    

    but this does:

    
    

    because the SelectedIndex is set to the SelecteIndexValue through binding.

    To avoid this and still keep the bindings in your markup set the SelectedIndexValue of your dataContext object to -1 before binding (Before InitializeComponent() is called in your form constructor).

    Hope this helps.

提交回复
热议问题