2016-07-17 26 views
1

我在我的第一個更大的WPF程序,許多Windows和ViewModel,並可以幫助自己,直到現在。
起初我有2個列表,一個List<double> Factor,一個List<string> Name,我試圖綁定兩個ListView。它沒有工作,我認爲一本字典會更好。C#WPF綁定字典到列表視圖

所以,現在我有一個Dictionary<string, double> IngList = new Dictionary<string, double>(); 點擊一個按鈕後,2個文本框的內容應該被添加到字典,然後顯示在我的ListView。

(法典只是一小部分,否則將需要多達多大的空間)

我的視圖模型:

namespace FoodsLib.ViewModel 
{ 
    public class ViewModelAddRecipeWindow : ViewModelBase, INotifyPropertyChanged 
    { 

     public ViewModelAddRecipeWindow(IViewControl vctrl) 
     { 
       AddIngredientCmd = new RelayCommand(() => 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<string, double> IngList = new Dictionary<string, double>(); 


    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; } 
     } 
} 

而我的觀點:

<Grid Grid.Row="2" Grid.Column="3" HorizontalAlignment="Left" 
        Margin="5,5,5,5" Grid.ColumnSpan="2" Grid.RowSpan="6"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="Auto"/> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="100"></ColumnDefinition> 
        <ColumnDefinition Width="240"></ColumnDefinition> 
       </Grid.ColumnDefinitions> 
       <TextBox x:Name="tbAddFood" Grid.Row="0" Grid.Column="1" Text="{Binding IngredientR, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" 
        Width="160" Margin="5" HorizontalAlignment="Left"/> 
       <TextBox x:Name="tbAddFoodAmount" Grid.Row="1" Grid.Column="1" Text="{Binding Factor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
        Width="50" Margin="5" HorizontalAlignment="Left"/> 

       <Label Grid.Row="0" Grid.Column="0" Content="Ingredients" HorizontalAlignment="Right" 
        Margin="5" Foreground="#ebeff4"/> 
       <Label Grid.Row="1" Grid.Column="0" Content="Amount" HorizontalAlignment="Right" 
        Margin="5" Foreground="#ebeff4"/> 



       <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"/> 

       <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"> 
        <ListView.View> 
         <GridView> 
          <GridView.ColumnHeaderContainerStyle> 
           <Style> 
            <Setter Property="FrameworkElement.Visibility" Value="Collapsed"/> 
           </Style> 
          </GridView.ColumnHeaderContainerStyle> 
          <GridViewColumn Width="Auto" DisplayMemberBinding="{Binding Key, Mode=OneWay}"></GridViewColumn> 
          <GridViewColumn Width="Auto" DisplayMemberBinding="{Binding Value, Mode=OneWay}"></GridViewColumn> 
         </GridView> 
        </ListView.View> 
       </ListView> 

      </Grid> 

我去Dictionary填充兩個項目的位置,但ListView不會顯示任何內容。

我不是要求一個完整的代碼,只是一個「如何」和「爲什麼」。 TY

編輯:

使用ObservableCollection訣竅。

public ObservableCollection<tempIng> _ListMF = new 
ObservableCollection<tempIng>(); 
     public ObservableCollection<tempIng> ListMF 
     { 
      get { return _ListMF; } 
      set { _ListMF = value; OnPropertyChanged("_ListMF"); } 
     } 

帶有一個新的類tempIng,單擊「Add」後獲取Name和Factor。

public class tempIng 
{ 
    public string Name { get; set; } 

    public double Factor { get; set; } 

    public override string ToString() 
    { 
     return this.Name + ", " + this.Factor ; 
    } 

} 

泰,我設置好的DataContext的在.xaml.cs
難道還用List工作?

+0

這行代碼將無法編譯它缺少一個右括號:AddIngredientCmd =新RelayCommand(()=> AddToList() _viewControl = VCTRL; – jdweng

回答

1

您只能綁定到屬性。請嘗試將列表中的

public ObservableCollection<KeyValuePair> IngList { get; private set; } 

我還沒有看到你設置的DataContext所以這裏有這樣一個例子也如果這聽起來新。

<Control> 
    <Control.Resources> 
     <ViewmodelNamespace:ViewModelAddRecipeWindow x:Key="ReceipieViewModel"/> 
    </Control.Resources> 

    <Grid DataContext="{StaticResource ReceipieViewModel}"> 
     <ListView ItemsSource="{Binding IngList}"/> 
    </Grid> 
    </Control> 

這裏是一個Tutorial on binding to a list in WPF

+0

謝謝查看「編輯:」下問題的更改 –