title: WPF/MVVM Abstract Common Methods
body: <p>SO Community, please bear with me as I am a newbie and I'm still learning(slowly). I have tried to implement absolutely every potential solution on the net with no success. I think I've completely confused myself with DependencyMethods, DependencyProperties, RelayCommands, ICommand, BaseViewModels, etc........</p>
  
  <p>Here's my scenario... I have a MainWindow(MainWindow) with a Frame containing a UserControl(SampleUC). The MainWindow and UserControl DataContexts are pointed to their respective ViewModels(MainWindowVM, SampleUCVM). MainWindowVM and SampleUCVM are children of a Base ViewModel(BASEVM) which uses INotifyPropertyChanged through an ObservableCollection helper class. There is a Combobox inside SampleUC that holds the Facilities ObservableCollection constructed in SampleUCVM with method "fillFacility" and stores the SelectedFacilityNum when selected in the Combobox with the "GetFacilityNum()" method.</p>
  
  <p>I would like to pull out the methods "fillFacility" and "GetFacilityNum" from the SampleUCVM and place them either in the BASEVM or a separate class where they can be accessed and used by other ViewModels. They will be used repeatedly throughout my project. Would someone be able to explain to me how to do this? Thank you for all your help and patience!</p>
  
  <p><strong>SampleUC.xaml</strong></p>
  
  <pre><code>    &lt;Grid&gt;
      &lt;Label Content="Facility" HorizontalAlignment="Left" Margin="10,32,0,0" VerticalAlignment="Top" Width="87" Height="27"/&gt;
      &lt;ComboBox Name="cboFacilities" 
                HorizontalAlignment="Left" Margin="119,37,0,0" VerticalAlignment="Top" Width="120"
                DisplayMemberPath="FacilityName"
                SelectedValuePath="FacilityName"
                ItemsSource="{Binding Facilities}"
                SelectedValue="{Binding SelectedFacility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p><strong>SampleUCVM</strong></p>
  
  <pre><code>public class SampleUCVM : BASEVM
  {
  
      #region MySQL Connection
  
      const string dbConnectionString = @"datasource=localhost;port=3306;Initial Catalog='optest1a1';username=root;password=";
  
      #endregion
  
      private ObservableCollection&lt;Facilities&gt; _facilitiesList;
      private string _selectedFacility;
      private int _selectedFacilityNum;
  
      public ObservableCollection&lt;Facilities&gt; Facilities
      {
          get { return _facilitiesList; }
          set
          {
              SetProperty(ref _facilitiesList, value, () =&gt; Facilities);
          }
      }
  
      public string SelectedFacility
      {
          get { return _selectedFacility; }
          set
          {
              SetProperty(ref _selectedFacility, value, () =&gt; SelectedFacility);
              if (_selectedFacility != null)
              {
                  GetFacilityNum();
              }
          }
      }
  
      public int SelectedFacilityNum
      {
          get { return _selectedFacilityNum; }
          set { SetProperty(ref _selectedFacilityNum, value, () =&gt; SelectedFacilityNum); }
      }
  
      public SampleUCVM()
      {
          Facilities = new ObservableCollection&lt;Facilities&gt;();
          fillFacilities();
      }
  
      private void fillFacilities()
      {
          using (MySqlConnection con = new MySqlConnection(dbConnectionString))
          {
              Facilities = new ObservableCollection&lt;Facilities&gt;();
              con.Open();
              string Query = "SELECT * FROM facilities";
              MySqlCommand createCommand = new MySqlCommand(Query, con);
              MySqlDataReader dr = createCommand.ExecuteReader();
              int count = 1;
              while (dr.Read())
              {
                  string FacilityName = dr.GetString(1);
                  Facilities facilityname = new Facilities(count, FacilityName);
                  Facilities.Add(facilityname);
                  count++;
              }
              con.Close();
          }
      }
  
      private void GetFacilityNum()
      {
          if (SelectedFacility != null)
          {
              using (MySqlConnection con = new MySqlConnection(dbConnectionString))
              {
                  con.Open();
                  string Query = "SELECT Facility_ID_Num FROM facilities WHERE Facility_Name='" + SelectedFacility + "' ";
                  MySqlCommand createCommand = new MySqlCommand(Query, con);
                  MySqlDataReader dr = createCommand.ExecuteReader();
                  int count = 1;
                  while (dr.Read())
                  {
                      int FacilityNum = dr.GetInt32(0);
                      SelectedFacilityNum = FacilityNum;
                      count++;
                  }
                  con.Close();
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>BASEVM</strong></p>
  
  <pre><code>    public class BASEVM : ObservableObject
  {
      public BASEVM()
      {
  
      }
  }
  </code></pre>