Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to avoid first item selection by default in wpf Listbox?

Tags:

wpf

xaml

listbox

I want to avoid first item selection that happens by default in Listbox when application loads. I tried with

SelectedIndex= -1

but it did not work. I do not want any item to be selected when ListBox loads until user selects any item from Listbox. I have IsSynchronization set to true but making it false also do not solve my problem. Though i have to keep IsSychronization set to true always. But thats ok. i did google but could not find any relevant answer.

Can anyone tell me how to do this ?

like image 978
Vjendra Gaorh Avatar asked Jan 10 '13 12:01

Vjendra Gaorh


1 Answers

I had this happen to me. When I bound ListBox.ItemsSource directly to a collection in my view model, the re was nothing selected by default.

When I switched to using a CollectionViewSource as the ListBox.ItemsSource, suddenly the first item was getting selected by default.

It turns out this was due to ListBox.IsSynchronizedWithCurrentItem. Setting that to

IsSynchronizedWithCurrentItem="False"

on the ListBox restored the desired behavior of having nothing selected by default.

like image 163
Ken Budris Avatar answered Sep 21 '22 06:09

Ken Budris