locked
Auto-select listbox item if there is only 1 item?

    Question

  • Hey guys,

    I need some help with auto selecting an item from a listbox if it consists of only one item.

    In my program I list the removable storage devices and let the user select one.  However, if there is only 1 device found, I want the program to auto select that for the user.

    All help is greatly appreciated.  Thanks in advance.
    • Edited by Wglinki Monday, May 19, 2014 9:08 PM
    Monday, May 19, 2014 8:57 PM

Answers

  • Hi,

    If your listbox if  consists of only one item and you want to make the listbox selected the item. You can use Selector.SelectedIndex property or Selector.SelectedItem property. About Selector.SelectedIndex property you can set the listbox selectedindex to 0:

    DeviceList.SelectedIndex=0 

    About Selector.SelectedItem property you can code like below:

    var deviceInfo = _deviceInfoCollection[0] as DeviceInformation ;

    DeviceList.SelectedItem=deviceInfo;

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; 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.

    • Marked as answer by Wglinki Tuesday, May 20, 2014 12:37 PM
    Tuesday, May 20, 2014 2:27 AM

All replies

  • Here is my current code:

    private async void setSaveLocation() { _deviceInfoCollection = await DeviceInformation.FindAllAsync(StorageDevice.GetDeviceSelector()); if (_deviceInfoCollection.Count > 0) { var items = new List<object>(); foreach (DeviceInformation deviceInfo in _deviceInfoCollection) { items.Add(new { Name = deviceInfo.Name, }); } DeviceList.ItemsSource = items; DeviceSelector.Visibility = Visibility.Visible; if (items.Count == 1) { var deviceInfo = _deviceInfoCollection[/* NEED SELECTOR CODE FOR DEVICE 0 HERE */]; storage = StorageDevice.FromId(deviceInfo.Id); DeviceSelector.Visibility = Visibility.Collapsed; } } } async private void DeviceList_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e) { DeviceSelector.Visibility = Visibility.Collapsed; if (_deviceInfoCollection == null) { return; // not yet populated } var deviceInfo = _deviceInfoCollection[DeviceList.SelectedIndex]; storage = StorageDevice.FromId(deviceInfo.Id);

    }

    Basically I need a replacement for DeviceList.SelectedIndex that doesn't require user input (device auto selects option 0).  I think that's what I need.

    Monday, May 19, 2014 9:06 PM
  • Hi,

    If your listbox if  consists of only one item and you want to make the listbox selected the item. You can use Selector.SelectedIndex property or Selector.SelectedItem property. About Selector.SelectedIndex property you can set the listbox selectedindex to 0:

    DeviceList.SelectedIndex=0 

    About Selector.SelectedItem property you can code like below:

    var deviceInfo = _deviceInfoCollection[0] as DeviceInformation ;

    DeviceList.SelectedItem=deviceInfo;

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; 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.

    • Marked as answer by Wglinki Tuesday, May 20, 2014 12:37 PM
    Tuesday, May 20, 2014 2:27 AM