This project has moved and is read-only. For the latest updates, please go here.

How to hookup a RelayCommand to SelectedItem in ListBox

Jul 12, 2013 at 11:14 PM
I have this code
  public RelayCommand GoBackToAddPricePage { get; set; }

        public const string SelectedStorePropertyName = "SelectedStore";

        private Store selectedStore;

        public Store SelectedStore
                return selectedStore;


                if (value != null)
                    selectedStore = value;
                    GoBackToAddPricePage = new RelayCommand(goBackToAddPricePage);


        private void goBackToAddPricePage()
            Messenger.Default.Send(new Uri("/AddProductPrice.xaml", UriKind.Relative), "NavigationRequest");
I followed these instructions to navigate back to a previous page but not sure how to do this as I want to leave to go back to my other view in my SelectedStore(which is bound to selectedItem).

I tired just calling it directly but that failed so not sure if I need to hookup relaycommand somehow.
Jul 15, 2013 at 4:11 PM
I'm not entirely certain what your trying to achieve here. Can you not just bind your combobox to a property on your Viewmodel which holds your selected store? You could then navigate back. I haven't used the WPF navigation service in ages, the Prism one is probably better. I would imagine you have a parent view model where you could keep the selected store so your app is context aware.

Bind Your selected Item to a your VM:

To use interactions you need this in your page namespaces:
<telerik:RadComboBox x:Name="radcombo_theme"
                                         SelectedValue="{Binding SelectedTheme}"
                                         ItemsSource="{Binding Source={StaticResource viewSource}}"
                                         ItemTemplate="{StaticResource ThemeComboTemplate}"
                                         Grid.Column="1" Height="23" VerticalAlignment="Top">
                            <i:EventTrigger EventName="SelectionChanged">
                                <mvvm:EventToCommand Command="{Binding commandThemeChanged}" PassEventArgsToCommand="True" />

In your vm:

 public class MyViewModelName: GalaSoft.MvvmLight.ViewModelBase, ICleanup

     public XmlNodeItem SelectedTheme { get; set; }

     public MyViewModelName()
               this.commandThemeChanged = new RelayCommand<object>((s) => 
                                               System.Windows.Controls.SelectionChangedEventArgs args = s as System.Windows.Controls.SelectionChangedEventArgs;
                                               if (args != null) { if (args.AddedItems.Count > 0) 
                                                   _selectedTheme = args.AddedItems[0] as XmlNodeItem (need your bound type here);
Not sure if this helps what shows how you can keep the value of your SelectedStore in the VM.