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>