WPF ComboBox SelectionChanged event for do not shoot team

I have the following XAML ComboBoxthat has an event handler SelectionChangedwith code-back and another Command ViewModel property. I set the property SelectedIndexto 0. Now, when I start the project, the code-code handler is called, but it Commanddoes not execute. I want to be Commandexecuted for SelectedIndex=0when the view is first 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 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) { }
+4
source share
1 answer

Snap SelectedValueto Propertyin view model.

In the block, Property set{...}execute your logic or call

ListTypeComboSelectionChangedCmdExec(value)

. ComboBox SelectedItem MVVM

+1

Source: https://habr.com/ru/post/1533220/


All Articles