ListItem Command and Event Aggregator Silverlight (prism)

Nov 2, 2009 at 2:24 AM
Edited Nov 2, 2009 at 2:47 PM

Hi

I am creating a prism2 Application mainly it contains 2 modules.

In module one i am going to place Images(around 10) using List Item and in Module 2 I Place an Image Control.

when i select a image in module one it should be displayed in module 2.

My Problem is am not able to generated a command Event of ListBox Selection Changed and Not able to Execute and publist the event.

Any help is Appreciated with a little sample code to gennerate Listitem Command Event (SilVerlight).

 

For Button  Command.Click.CommandName and Path by using commands namespace Is there any kind for ListBox selectionchange.

 

Thanks in advance.

Nov 2, 2009 at 7:15 PM

Hi

In my personal opinion there is no need to create a command to execute an action when a ListBox item is clicked. Instead you can bind the ListBox’s SelectedItem and publish the event through Event Aggregator every time the selected item changes.

If you do decide to use a command instead of plain binding you can use this code snippet. This sample application uses a command to execute an action when a new Grid item is selected, so you might find it useful.

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

 

Nov 3, 2009 at 4:21 PM
Edited Nov 3, 2009 at 4:25 PM
dschenkelman wrote:

Hi

In my personal opinion there is no need to create a command to execute an action when a ListBox item is clicked. Instead you can bind the ListBox’s SelectedItem and publish the event through Event Aggregator every time the selected item changes.

If you do decide to use a command instead of plain binding you can use this code snippet. This sample application uses a command to execute an action when a new Grid item is selected, so you might find it useful.

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

 

 Hi Damian,

Thanks a lot for Reply and your code snippet is awesome and i am very much comfortable regarding prism while going through.

I am able to create application and build it but not able to publish the event i tried in many different ways I tried with INotifyProperty change but noluck .

                  

VIEWMODEL(I CreateModel init)

public class ThumbViewModel 
    {
        public ICommand SelectionChanged { get; private set; }
        IEventAggregator _eventAggregator;
        public ThumbViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            this.SelectionChanged = new DelegateCommand(OnSearch);
            Stories = new ObservableCollection();
            Stories.Add(new People(){Title = "/DragDrop.Thumb;Component/Images/Sunset.jpg"});
            Stories.Add(new People() { Title = "/DragDrop.Thumb;Component/Images/Water lilies.jpg" });
            //OnPropertyChanged("Title");
        }
       
        private void OnSearch(People people)
        {
            CustomerSelected customerSelected = new CustomerSelected()
            {
                image = people.Title
                
            };
           _eventAggregator.GetEvent().Publish(customerSelected);
        }
       

        //#region INotifyPropertyChanged Members

        //public event PropertyChangedEventHandler PropertyChanged;

        //protected void OnPropertyChanged(string propertyName)
        //{
        //    if (this.PropertyChanged != null)
        //    {
        //        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        //    }
        //}

        //#endregion


        public ObservableCollection Stories
        {
            get;
            private set;
        }

    }
    public class People
    {
        public string Title { get; set; }
    }

 XAML

 

 

<ListBox ItemsSource="{Binding Stories}" y:SelectionChanged.Command="{Binding Command}" y:SelectionChanged.CommandParameter="{Binding Path=SelectedItem}">

 

<ListBox.ItemTemplate>

 

<DataTemplate>

 

 

<StackPanel Orientation="Horizontal"> <Image Source="{Binding Title}" Height="150" Width="Auto"/>

 

</StackPanel>

 

</DataTemplate>

 

</ListBox.ItemTemplate>

 

</ListBox>

 
            
                
                    
                    
                       
                
            
        

 

Nov 3, 2009 at 10:22 PM

Hi

From the above code I see you are using the following syntax to publish the event:

_eventAggregator.GetEvent().Publish(customerSelected);

To get an event and publish it you should use code like this (having previously created the CustomerSelectedEvent):

_eventAggregator.GetEvent<CustomerSelectedEvent>().Publish(customerSelected);

This is explained with much more detail in the following articles of the Prism documentation:

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman