How to ensure that no item is selected in a ListBox data list?

OK, this should be a duplicate question, but I can’t find the answer: I have a list related to collection binding. By default, items [0] are selected. How can I prevent this from happening to ensure that the SelectionChanged event is raised every time I click the ListBoxItem button?

EDIT: I already rejected the SelectedIndex = -1 route, but I tried again: setting SelectedIndex to -1 in the Listbox constructor (or as an attribute in XAML) does not work. It seems that the listbox is populated after initialization, and selectedindex will become 0 after all. This story is for setting SelectedItem to null;

I tried this:

<ListBox ItemsSource="{Binding Value}" 
         SelectionChanged="ListBox_SelectionChanged"
         IsSynchronizedWithCurrentItem="True"
         Loaded="ListBox_Loaded">
</ListBox>

with:

 private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if(e.AddedItems.Count==0)return;//Prevents stackoverflow! ;-)
        ((ListBox)e.OriginalSource).SelectedItem=null;
    }

private void ListBox_Loaded(object sender, RoutedEventArgs e)
{
    ((ListBox) sender).SelectedItem = null;
}

, , , .... , IsSynchronizedWithCurrentItem.

, .

: master-detail IsSynchronizedWithCurrentItem true, . , master-detail,

+4
6

CollectionView ItemsSource . WPF , :

XAML:

<Window x:Class="ListBoxDataBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <ListBox 
        ItemsSource="{Binding Path=Value, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" 
        SelectionChanged="ListBox_SelectionChanged"
        IsSynchronizedWithCurrentItem="True"
        >
    </ListBox>
</Grid>
</Window>

CS:

public partial class Window1: Window {
    public IEnumerable Value {
        get { return (IEnumerable)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(IEnumerable), typeof(Window1), new UIPropertyMetadata(null));


    public Window1() {
        InitializeComponent();
        var view = new TheCollectionView();
        view.MoveCurrentTo(null);
        this.Value = view;
    }

    private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
        MessageBox.Show("Selection changed");
    }
}

public class TheCollectionView: CollectionView {
    public TheCollectionView(): base(new TheCollection()) {
    }
}

public class TheCollection: List<string> {
    public TheCollection() {
        Add("string1");
        Add("string2");
        Add("string3");
    }
}
+2

ListBox , . IsSynchronizedWithCurrentItem = "True" , , SelectionChanged , - .

IsSynchronizedWithCurrentItem true, , selectedIndex ListBox.Items.CurrentItem ListBox.Items. ListBox.Items CurrentItem -1 null - , CurrentItem CurrentIndex readonly.

, J

...

" , IsSynchronizedWithCurrentItem. , ".

, , .

+6
listbox.selectedIndex = -1

( -1 ), , , listview , , SelectedIndex - . , SelectedIndex XAML.

+2

You need to set the index to -1, which means that there will be nothing in the list. In the SelectionChanged event, set the Index Index property to -1. A.

Hope this helps, Regards, Tom.

+1
source

What about dumping the SelectionChanged event handler and binding data to a dependency property or notifying the SelectedValue property instead?

+1
source

You can use the Tap event instead of SelectionChanged:

<ListBox  ItemsSource="{Binding Data}" Tap="ListBox_Tap" ... />
0
source

All Articles