title: WPF User Control Data Binding Not Working
body: <p>I'm creating a simple User Control combining a popup with a text view, nothing crazy. When I set it up in a window at first to style it all out, it worked perfectly, but when I moved it into a User Control to actually finish it up, it won't work correctly any more. </p>
  
  <p>I pass a min and max value into the control and then it automatically creates a list of numbers to pick from in that range. In the User Control, the list of numbers doesn't get bound correctly, who knows why. Maybe someone can take a look at my code. </p>
  
  <p>I've read a bunch of other questions about this, but don't really know what's happening. I'm not getting any errors in my output window, so no clues there. Anyway, here's the code -</p>
  
  <p>UserControl.xaml</p>
  
  <pre><code>&lt;UserControl x:Class="UserControl1"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           x:Name="Me"
           DataContext="{Binding RelativeSource={RelativeSource Self}}"&gt;
  
  &lt;StackPanel&gt;
      &lt;TextBox Name="myTextbox"
               Height="30"
               Margin="0"
               FontSize="14"
               IsReadOnly="True"
               Padding="5,2"
               Text="{Binding Value}" /&gt;
      &lt;Popup x:Name="myPopup"
             PlacementTarget="{Binding ElementName=myTextbox}"
             StaysOpen="True"&gt;
          &lt;Popup.Style&gt;
              &lt;Style TargetType="{x:Type Popup}"&gt;
                  &lt;Style.Triggers&gt;
                      &lt;DataTrigger Binding="{Binding ElementName=myTextbox, Path=IsFocused}" Value="True"&gt;
                          &lt;Setter Property="IsOpen" Value="True" /&gt;
                      &lt;/DataTrigger&gt;
                  &lt;/Style.Triggers&gt;
              &lt;/Style&gt;
          &lt;/Popup.Style&gt;
  
          &lt;StackPanel&gt;
              &lt;ListView Name="myListView"
                        Height="100"
                        MaxHeight="300"
                        ItemsSource="{Binding List,
                                              UpdateSourceTrigger=PropertyChanged}"
                        SelectionChanged="ListView_SelectionChanged"&gt;
                  &lt;ListView.ItemTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;Label Width="100"
                                 Height="30"
                                 Margin="0"
                                 Content="{Binding}"
                                 FontFamily="Segoe UI"
                                 FontSize="14"
                                 Padding="5,2" /&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ListView.ItemTemplate&gt;
              &lt;/ListView&gt;
              &lt;Button Width="200" Height="30" /&gt;
          &lt;/StackPanel&gt;
      &lt;/Popup&gt;
  
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p></p>
  
  <p>UserControl.xaml.vb</p>
  
  <pre><code>Imports System.ComponentModel
  Imports System.Linq.Expressions
  Imports System.Collections.ObjectModel
  
  Class UserControl1
  
  ' Dependency Properties
  Public Shared ReadOnly ListProperty As DependencyProperty = DependencyProperty.Register("List", GetType(ObservableCollection(Of Integer)), GetType(MainWindow))
  Public Shared ReadOnly ValueProperty As DependencyProperty = DependencyProperty.Register("Value", GetType(Integer), GetType(MainWindow))
  Public Shared ReadOnly MaxValueProperty As DependencyProperty = DependencyProperty.Register("MaxValue", GetType(Integer), GetType(MainWindow))
  Public Shared ReadOnly MinValueProperty As DependencyProperty = DependencyProperty.Register("MinValue", GetType(Integer), GetType(MainWindow))
  
  ' Properties
  Public Property List As ObservableCollection(Of Integer)
      Get
          Return DirectCast(GetValue(ListProperty), ObservableCollection(Of Integer))
      End Get
      Set(value As ObservableCollection(Of Integer))
          SetValue(ListProperty, value)
      End Set
  End Property
  
  Public Property Value As Integer
      Get
          Return DirectCast(GetValue(ValueProperty), Integer)
      End Get
      Set(value As Integer)
          SetValue(ValueProperty, value)
  
      End Set
  End Property
  
  Public Property MaxValue As Integer
      Get
          Return DirectCast(GetValue(MaxValueProperty), Integer)
      End Get
      Set(value As Integer)
          SetValue(MaxValueProperty, value)
      End Set
  End Property
  
  Public Property MinValue As Integer
      Get
          Return DirectCast(GetValue(MinValueProperty), Integer)
      End Get
      Set(value As Integer)
          SetValue(MinValueProperty, value)
      End Set
  End Property
  
  Private Sub ListView_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
      Value = List(myListView.SelectedIndex)
  End Sub
  
  Private Sub UserControl1_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
      List = New ObservableCollection(Of Integer)
  
      ' Add all available numbers into the list
      For iCounter As Integer = MinValue To MaxValue
          List.Add(iCounter)
      Next
  
      ' Set the selected index on the list for the value
      myListView.SelectedIndex = Value - MinValue
  End Sub
  End Class
  </code></pre>
  
  <p>Just for reference, when I tested this out, I set the Min and Max values myself in both the window and usercontrol setup.</p>