title: Fire a command when WPF ComboBox changes
body: <p>In my ComboBox I have few different languages which are loaded from a database (through Binding). When the selection in the ComboBox changes to a different language and the User clicks on the OK button, I want to do something simple like show a new window. I want to realize this with a <a href="http://www.wpf-tutorial.com/commands/using-commands/" rel="nofollow noreferrer">WPF Commands</a> (Name_CanExecute and Name_Executed). I was playing all day yesterday and couldn't make it work. I am new to Wpf and MVVM pattern. </p>
  
  <p>I would be very thankful if someone could show me how to do it. Any help is appreciated.</p>
  
  <p><a href="https://i.stack.imgur.com/fVrfW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fVrfW.png" alt="enter image description here"></a></p>
  
  <p>My <strong>.xaml code</strong> in the View looks like this:</p>
  
  <pre><code>&lt;UserControl x:Class="***.***.Modules.Localization.Views.LocalizationManager"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:local="clr-namespace:***.***.Modules.Localization.Services"
           xmlns:viewModels="clr-namespace:***.***.Modules.Localization.ViewModels"
           Height="270" Width="230"
           mc:Ignorable="d"&gt;
  &lt;UserControl.Resources&gt;
      &lt;viewModels:LocalizationViewModelLocator x:Key="LocalizationViewModelLocator"/&gt;
  &lt;/UserControl.Resources&gt;
  &lt;UserControl.DataContext&gt;
      &lt;Binding Mode="OneWay" Path="LocalizationViewModel" Source="{StaticResource LocalizationViewModelLocator}"/&gt;
  &lt;/UserControl.DataContext&gt;
  
  &lt;Grid Background="Gainsboro"&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="*" /&gt;
          &lt;RowDefinition Height="Auto" /&gt;
      &lt;/Grid.RowDefinitions&gt;
  
      &lt;StackPanel Orientation="Horizontal" Grid.Row="1" Margin="10,10"&gt;
          &lt;TextBlock FontSize="13" FontFamily="Verdana" Text="Language: " /&gt;
  
          &lt;ComboBox Name="CboLanguageSelector" BorderThickness="1" MinWidth="105" ItemsSource="{Binding Languages}" /&gt;
  
          &lt;Button&gt;OK&lt;/Button&gt;
  
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>and my <strong>code in ViewModel</strong> looks like this:</p>
  
  <pre><code> public class LocalizationViewModel : LocalizableViewModel
      {
          //the available languages from database are saved here
          public ObservableCollection&lt;string&gt; Languages { get; private set; }
          private readonly IDataAccessUnitOfWorkFactory dataAccessUnitOfWorkFactory;
          public LocalizationViewModel(ILocalizationService localizationService,
                                   IDataAccessUnitOfWorkFactory dataAccessUnitOfWorkFactory) : base(localizationService)
      {
          this.dataAccessUnitOfWorkFactory = dataAccessUnitOfWorkFactory;
          LoadLanguagesFromCtlangTable();
  
      }
      /// &lt;summary&gt;
      /// Get the available languages from the CTLANG table
      /// &lt;/summary&gt;
      private void LoadLanguagesFromCtlangTable()
      {
          using (var unitOfWork = dataAccessUnitOfWorkFactory.Create())
          {
              string query = "SELECT languagename FROM ctlang ORDER BY languagename";
              Languages = new ObservableCollection&lt;string&gt;(unitOfWork.OwEntities.Database.SqlQuery&lt;string&gt;(query));
          }
      }
      //Implement Command_Execute and Command_CanExecute
  
  
  }
  </code></pre>