8

I have the following XAML of a ComboBox which has a code-behind SelectionChanged event handler and another Command property of the ViewModel. I have set the SelectedIndex property to 0. Now when I run the project, the code-behind handler is invoked, but the Command is not executed. What I want is that the Command should be executed for SelectedIndex=0 the first time the View is loaded.

<ComboBox Name="listComboBox" SelectionChanged="listComboBox_SelectionChanged" SelectedIndex="0" SelectedValuePath="Content" Margin="5,0" Height="35" Width="150" VerticalAlignment="Center" HorizontalAlignment="Left">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding ListTypeComboSelectionChangedCmd}" CommandParameter="{Binding ElementName=listComboBox, Path=SelectedValue}"/>
        </i:EventTrigger>
     </i:Interaction.Triggers>
     <ComboBoxItem Content="ItemOne" />
     <ComboBoxItem Content="ItemTwo" />
     <ComboBoxItem Content="ItemThree" />
</ComboBox>

Update

Code-behind event handler:

private void listComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { }

ICommand object:

public ICommand ListTypeComboSelectionChangedCmd 
{ 
    get { return new RelayCommand<string>(ListTypeComboSelectionChangedCmdExec); }
    private set;
}

ICommand Handler:

private void ListTypeComboSelectionChangedCmdExec(string listType) { }
Athafoud
  • 2,898
  • 3
  • 40
  • 58
Lucifer
  • 2,317
  • 9
  • 43
  • 67
  • Why is this for? If you want something to happen just after the view as loaded why not do it *when* in loads? Also, no event will be fired for a SelectedIndex set in xaml at creation (not runtime). – Xavier Huppé Mar 24 '14 at 20:10
  • You have an event and a command for the same thing. Get rid of the inline event call. – Xcalibur37 Mar 25 '14 at 00:29
  • @Sinity, if the selectionchanged event for the first ComboBoxItem is fired right after the view is loaded, then why shouldn't the command execute ? – Lucifer Mar 25 '14 at 05:44
  • @Xcalibur37, even after I remove the SelectionChanged event handler the command does not execute. – Lucifer Mar 25 '14 at 05:45
  • @Lucifer, if so, post the code which fire the event you're talking about (after the view as loaded). – Xavier Huppé Mar 25 '14 at 12:41
  • @Sinity, I have updated my question as per your request, but I not sure how much will it help. – Lucifer Mar 25 '14 at 16:07
  • What are you trying to do in ListTypeComboSelectionChangedCmdExec. – Michal Ciechan Mar 26 '14 at 16:18

2 Answers2

9

Bind SelectedValue to a Property on the view model.

In the Property set{...} block do your logic there or call

ListTypeComboSelectionChangedCmdExec(value)

See Binding ComboBox SelectedItem using MVVM

Community
  • 1
  • 1
Michal Ciechan
  • 13,492
  • 11
  • 76
  • 118
  • 5
    That's a smart solution, but I would have preferred to keep the setter clean. Also I was more interested in knowing as to why such a functionality doesn't work, where the SelectionChanged event fires but it does not execute the underlying Command. Thanks anyway. – Lucifer Mar 26 '14 at 18:56
  • Why not Bind SelectedValue to the Property, but in your view OnLoaded event handler, set it to the first value. Setter logic is sub optimal – Richard June Dec 05 '16 at 13:45
  • @Lucifer bit late of a reply, but my guess would be that the event gets fired before the behaviour gets 'Attached'. – Michal Ciechan Apr 20 '18 at 09:14
4

In my case, I use handler in the code behind and connect it to ModelView as below.

var viewModel = (MyViewModel)DataContext;
if (viewModel.MyCommand.CanExecute(null))
    viewModel.MyCommand.Execute(null);

Please check this link: Call Command from Code Behind

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Sangwon Choi
  • 218
  • 2
  • 11