ListBox SelectedItem Convention

Topics: Conventions
Sep 4, 2012 at 4:26 PM
Edited Sep 4, 2012 at 4:26 PM

Hi,

Can anyone please look at this example and see what is wrong with it. Based on the Caliburn.Micro documentation, if my listbox's name is "Items", then it should automatically bind the listbox to Items collection and the Selected item to SelectedItem property.

In below example, the listbox is getting binded to the Items collection, but when I change the selection in the listbox, I don't see the SelectedItem property in the view model getting updated. 

 

 public class MainViewModel : Conductor<string>.Collection.OneActive
    {
        private string _selectedItem;

        public MainViewModel()
        { 
            Items.Add("A");
            Items.Add("B");
            Items.Add("C");
        }

        public string SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                NotifyOfPropertyChange(() => SelectedItem);
            }
        }
    }
Here is the XAML:
 <ListBox x:Name="Items" Grid.Row="0"/>
Sep 4, 2012 at 4:30 PM
Edited Sep 4, 2012 at 4:33 PM

I suppose that the CM is binding by default to

Conductor<string>.Collection.OneActive.ActiveItem

Note that the ConventionManager supports the following formats for the selected item (check here):

        /// <summary>
        /// Derives the SelectedItem property name.
        /// </summary>
        public static Func<string, IEnumerable<string>> DerivePotentialSelectionNames = name =>
        {
            var singular = Singularize(name);
            return new[] {
                "Active" + singular,
                "Selected" + singular,
                "Current" + singular
            };
        };

So, ActiveItem, SelectedItem and CurrentItem are all valid property names used to bind the ListBox.SelectedItem.

In your view-model you have both ActiveItem and SelectedItem, due to the order in the array, the ActiveItem is picked before the SelectedItem.

Sep 4, 2012 at 5:08 PM

Thank you for your quick response and help. I see what is happening. But now I am trying to attach an event to list box's selection change event and I am getting following error message: 

Items collection must be empty before using ItemsSource.

 

 
public  MainViewModel : Conductor<string>.Collection.OneActive
    {
        public IEnumerable<string> Results { get; private set; }
        
        public MainViewModel()
        {
            Results = new BindableCollection<string> {"A", "B", "C"};

        }
 
        private string _selectedResult;

        public string SelectedResult
        {
            get { return _selectedResult; }
            set
            {
                _selectedResult = value;
                NotifyOfPropertyChange(() => SelectedResult);
            }
        }

        public void Load()
        {
            MessageBox.Show(SelectedResult);
        }

    }

 

And here is XAML

 

   <ListBox x:Name="Results">
                <i:EventTrigger EventName="SelectionChanged">
                    <cal:ActionMessage MethodName="Load"/>
                </i:EventTrigger>
            </ListBox>

 

Sep 4, 2012 at 5:13 PM

You are putting the trigger inside the listbox, you need to populate the i:Interactivity.Triggers collectionion instead.

Sep 4, 2012 at 5:34 PM

One again thank you