title: C# WPF Binding Dictionary to Listview
body: <p>I am on my first bigger WPF Program, many Windows and ViewModel, and could help myself just fine until now.<br>
  At first i had 2 List, one <code>List&lt;double&gt; Factor</code>, one <code>List&lt;string&gt; Name</code>, and I tried to bind both to one ListView. It didn't work, and I thought a Dictionary would be better.</p>
  
  <p>So now I have a <code>Dictionary&lt;string, double&gt; IngList = new Dictionary&lt;string, double&gt;();</code> 
  After Clicking a Button the Content of 2 TextBox should get added to the Dictionary and then Displayed on my ListView.</p>
  
  <p>(Just a small portion of the Code, else it would take up to much space)</p>
  
  <p>My ViewModel:</p>
  
  <pre><code>namespace FoodsLib.ViewModel
  {
      public class ViewModelAddRecipeWindow : ViewModelBase, INotifyPropertyChanged
      {
  
          public ViewModelAddRecipeWindow( IViewControl vctrl) 
          {
                  AddIngredientCmd = new RelayCommand(() =&gt; AddToList() 
                  _viewControl = vctrl;
                  }
          }
  
          protected IViewControl _viewControl;
  
  
  
     new public event PropertyChangedEventHandler PropertyChanged;
  
          new protected virtual void OnPropertyChanged(string propertyName)
          {
              PropertyChangedEventHandler handler = this.PropertyChanged;
              if (handler != null)
              {
                  var e = new PropertyChangedEventArgs(propertyName);
                  handler(this, e);
              }
          }
  
          protected string _ingredientR, _name, _stringtype, _stringCategory, _recipe;
  
              public double Factor
              {
                  get { return _factor; }
                  set
                  {
                      if (_factor != value)
                      {
                          _factor = value;
                          OnPropertyChanged("Factor");
                      }
                  }
              }
  
              public string IngredientR
              {
                  get { return _ingredientR; }
                  set
                  {
                      if (_ingredientR != value)
                      {
                          _ingredientR = value;
                          this.OnPropertyChanged("IngredientR");
                      }
                  }
              }
  
              Dictionary&lt;string, double&gt; IngList = new Dictionary&lt;string, double&gt;();
  
  
      public void AddToList()
              {
  
                  if (!IngList.ContainsKey(IngredientR))
                  {
                      IngList.Add(IngredientR, Factor);
                      _viewControl.ShowMessage("Added", "Ingredient");
                  }
                  else
                  {
                      _viewControl.ShowMessage("You can't add an Ingredient twice", "Ingredient Error.");
                      return;
                  }
  
              }
  
          public ICommand AddIngredientCmd { get; set; }
        }
  }
  </code></pre>
  
  <p>And my View:</p>
  
  <pre><code> &lt;Grid Grid.Row="2" Grid.Column="3" HorizontalAlignment="Left" 
                    Margin="5,5,5,5" Grid.ColumnSpan="2" Grid.RowSpan="6"&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="Auto"/&gt;
                      &lt;RowDefinition Height="Auto"/&gt;
                      &lt;RowDefinition Height="Auto"/&gt;
                      &lt;RowDefinition Height="Auto"/&gt;
                      &lt;RowDefinition Height="Auto"/&gt;
                      &lt;RowDefinition Height="Auto"/&gt;
                      &lt;RowDefinition Height="Auto"/&gt;
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="100"&gt;&lt;/ColumnDefinition&gt;
                      &lt;ColumnDefinition Width="240"&gt;&lt;/ColumnDefinition&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;TextBox x:Name="tbAddFood" Grid.Row="0" Grid.Column="1" Text="{Binding IngredientR, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"  
                       Width="160" Margin="5" HorizontalAlignment="Left"/&gt;
                  &lt;TextBox x:Name="tbAddFoodAmount" Grid.Row="1" Grid.Column="1" Text="{Binding Factor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  
                       Width="50" Margin="5" HorizontalAlignment="Left"/&gt;
  
                  &lt;Label Grid.Row="0" Grid.Column="0" Content="Ingredients"  HorizontalAlignment="Right"
                     Margin="5" Foreground="#ebeff4"/&gt;
                  &lt;Label Grid.Row="1" Grid.Column="0" Content="Amount"  HorizontalAlignment="Right"
                     Margin="5" Foreground="#ebeff4"/&gt;
  
  
  
                  &lt;Button x:Name="btAddIngredient" Grid.Row="1" Grid.Column="1"
                  Command="{Binding Path=AddIngredientCmd}" Content=" Add "  Margin="5,5,30,5" 
                      BorderBrush="#415578" BorderThickness="0" 
                      HorizontalAlignment="Center" Background="#415578" Foreground="#ebeff4"/&gt;
  
                  &lt;ListView x:Name="lbFoodList" Grid.Column="1" Grid.Row="2"  Margin="5" Height="120" VerticalAlignment="Top"
                           Foreground="#ebeff4" ItemsSource="{Binding IngList}"
                                           ScrollViewer.VerticalScrollBarVisibility="Visible" Grid.RowSpan="4"&gt;
                      &lt;ListView.View&gt;
                          &lt;GridView&gt;
                              &lt;GridView.ColumnHeaderContainerStyle&gt;
                                  &lt;Style&gt;
                                      &lt;Setter Property="FrameworkElement.Visibility" Value="Collapsed"/&gt;
                                  &lt;/Style&gt;
                              &lt;/GridView.ColumnHeaderContainerStyle&gt;
                              &lt;GridViewColumn Width="Auto" DisplayMemberBinding="{Binding Key, Mode=OneWay}"&gt;&lt;/GridViewColumn&gt;
                              &lt;GridViewColumn Width="Auto" DisplayMemberBinding="{Binding Value, Mode=OneWay}"&gt;&lt;/GridViewColumn&gt;
                          &lt;/GridView&gt;
                      &lt;/ListView.View&gt;
                  &lt;/ListView&gt;
  
              &lt;/Grid&gt;
  </code></pre>
  
  <p>I get to the point where the Dictionary gets filled with both items, but the ListView won't display anything.</p>
  
  <p>I am not asking for a complete Code, just a "how" and "why". TY</p>
  
  <p>Edit:</p>
  
  <p>Using an ObservableCollection did the trick. </p>
  
  <pre><code>public ObservableCollection&lt;tempIng&gt; _ListMF = new
  ObservableCollection&lt;tempIng&gt;();
          public ObservableCollection&lt;tempIng&gt; ListMF
          {
              get { return _ListMF; }
              set { _ListMF = value; OnPropertyChanged("_ListMF"); }
          }
  </code></pre>
  
  <p>with a new Class tempIng, which gets the Name and Factor after clicking "Add".</p>
  
  <pre><code>public class tempIng
  {
      public string Name { get; set; }
  
      public double Factor { get; set; }
  
      public override string ToString()
      {
          return this.Name + ", " + this.Factor ;
      }
  
  }
  </code></pre>
  
  <p>Ty, and I setted the DataContext in the .xaml.cs<br>
  Would it also work with a List ?</p>