title: Bind SelectedValue of UserControl to ViewModel
body: <p>In my solution; I have two projects:  One is a WPF UserControl Library, and the other is a WPF Application.</p>
  
  <p>The usercontrol is pretty straightforward; it's a label and a combo box that will show the installed printers.</p>
  
  <p>In the WPF application; I want to use this usercontrol.  The selected value will be stored in user settings.</p>
  
  <p>The problem I'm having is that I can't seem to get the proper binding to work.  What I need to happen is to be able to set the SelectedValue of the UserControl when the MainWindow loads; as well as access the SelectedValue of the UserControl when I go to save my settings.</p>
  
  <p>My code is below, could someone point me in the right direction?</p>
  
  <p>PrintQueue user control:</p>
  
  <pre><code>&lt;UserControl x:Class="WpfControls.PrintQueue"
               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:wpfControls="clr-namespace:WpfControls"
               mc:Ignorable="d"&gt;
      &lt;UserControl.DataContext&gt;
          &lt;wpfControls:PrintQueueViewModel/&gt;
      &lt;/UserControl.DataContext&gt;
      &lt;Grid&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;Label Content="Selected Printer:"&gt;&lt;/Label&gt;
              &lt;ComboBox ItemsSource="{Binding Path=PrintQueues, Mode=OneWay}" DisplayMemberPath="Name" SelectedValuePath="Name" Width="200" SelectedValue="{Binding Path=SelectedPrinterName, Mode=TwoWay}"&gt;&lt;/ComboBox&gt;
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>Print Queue Codebehind:</p>
  
  <pre><code>public partial class PrintQueue : UserControl
  {
      public static readonly DependencyProperty CurrentPrinterNameProperty =
          DependencyProperty.Register("CurrentPrinterName", typeof (string), typeof (PrintQueue), new PropertyMetadata(default(string)));
  
      public string CurrentPrinterName
      {
          get { return (DataContext as PrintQueueViewModel).SelectedPrinterName; }
          set { (DataContext as PrintQueueViewModel).SelectedPrinterName = value; }
      }
  
  
      public PrintQueue()
      {
          InitializeComponent();
          DataContext = new PrintQueueViewModel();
      }
  }
  </code></pre>
  
  <p>PrintQueue View Model:</p>
  
  <pre><code>public class PrintQueueViewModel : ViewModelBase
  {
      private ObservableCollection&lt;System.Printing.PrintQueue&gt; printQueues;
      public ObservableCollection&lt;System.Printing.PrintQueue&gt; PrintQueues
      {
          get { return printQueues; }
          set
          {
              printQueues = value;
              NotifyPropertyChanged(() =&gt; PrintQueues);
          }
      }
  
  
      private string selectedPrinterName;
      public string SelectedPrinterName
      {
          get { return selectedPrinterName; }
          set
          {
              selectedPrinterName = value;
              NotifyPropertyChanged(() =&gt; SelectedPrinterName);
          }
      }
  
      public PrintQueueViewModel()
      {
          PrintQueues = GetPrintQueues();
      }
  
  
      private static ObservableCollection&lt;System.Printing.PrintQueue&gt; GetPrintQueues()
      {
          var ps = new PrintServer();
          return new ObservableCollection&lt;System.Printing.PrintQueue&gt;(ps.GetPrintQueues(new[]
              {
                  EnumeratedPrintQueueTypes.Local,
                  EnumeratedPrintQueueTypes.Connections
              }));
      }
  }
  </code></pre>
  
  <p>Main Window:</p>
  
  <pre><code>&lt;Window x:Class="WPFApp.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:wpfControls="clr-namespace:WpfControls;assembly=WpfControls" xmlns:wpfApp="clr-namespace:WPFApp"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Window.DataContext&gt;
          &lt;wpfApp:MainWindowViewModel/&gt;
      &lt;/Window.DataContext&gt;
      &lt;Grid&gt;
          &lt;StackPanel&gt;
              &lt;wpfControls:PrintQueue CurrentPrinterName="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.PrinterName, Mode=TwoWay}"&gt;&lt;/wpfControls:PrintQueue&gt;
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Main Window View Model:</p>
  
  <pre><code>public class MainWindowViewModel : ViewModelBase
  {
      private string printerName;
  
      public string PrinterName
      {
          get { return printerName; }
          set
          {
              printerName = value;
              NotifyPropertyChanged(() =&gt; PrinterName);
          }
      }
  
      public MainWindowViewModel()
      {
          PrinterName = "Lexmark T656 PS3";
      }
  }
  </code></pre>