2017-04-17 90 views
-1

我是WPF的新手。我努力了一個星期,在點擊按鈕的datagrid中添加一行。WPF Datagrid雙向綁定:點擊按鈕添加行

我的數據網格被綁定到ObservableCollection<FilterCriteria> FilterCriteriaList

對不起,添加這樣的長度的代碼。作爲WPF的新手,我不確定我在這裏錯過了什麼。因此,試圖提供儘可能多的信息可能

這是我FilterCriteria類

public class FilterCriteria : INotifyPropertyChanged 
    { 
     private NORTHWNDEntities context = new NORTHWNDEntities(); 

     private string columnName; 
     public string ColumnName 
     { 
      get 
      { 
       return columnName; 
      } 
      set 
      { 
       columnName = value; 
       RaisePropertyChanged("ColumnName"); 
      } 
     } 

     private string _operator; 
     public string Operator 
     { 
      get 
      { 
       return _operator; 
      } 
      set 
      { 
       _operator = value; 
       RaisePropertyChanged("Operator"); 
      } 
     } 

     private string inputValue; 
     public string InputValue 
     { 
      get 
      { 
       return inputValue; 
      } 
      set 
      { 
       inputValue = value; 
       RaisePropertyChanged("InputValue"); 
      } 
     } 

     private string selectedTable; 
     public string SelectedTable 
     { 
      get 
      { 
       return selectedTable; 
      } 
      set 
      { 
       selectedTable = value; 
       RaisePropertyChanged("SelectedTable"); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     protected void RaisePropertyChanged([CallerMemberName]string propertyName = "") 
     { 
      var handler = PropertyChanged; 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 

這是我的ViewModel代碼

public class ExportTablesViewModel : INotifyPropertyChanged 
    { 
     private NORTHWNDEntities context = new NORTHWNDEntities(); 

     private ObservableCollection<DatabaseTable> tableNameList; 
     public ObservableCollection<DatabaseTable> TableNameList 
     { 
      get 
      { 
       return tableNameList; 
      } 
      set 
      { 
       tableNameList = value; 
       RaisePropertyChanged("TableNameList"); 
      } 
     } 

     private ObservableCollection<string> selectedTableColumnList; 

     public ObservableCollection<string> SelectedTableColumnList 
     { 
      get 
      { 
       return selectedTableColumnList; 
      } 
      set 
      { 
       selectedTableColumnList = value; 
       RaisePropertyChanged("SelectedTableColumnList"); 
      } 
     } 


     private ObservableCollection<FilterCriteria> filterCriteriaList; 

     public ObservableCollection<FilterCriteria> FilterCriteriaList 
     { 
      get 
      { 
       return filterCriteriaList; 
      } 
      set 
      { 
       filterCriteriaList = value; 
       RaisePropertyChanged("FilterCriteriaList"); 
      } 
     } 


     private string selectedTableName; 

     public string SelectedTableName 
     { 
      get 
      { 
       return selectedTableName; 
      } 
      set 
      { 
       selectedTableName = value; 
       RaisePropertyChanged("SelectedTableName"); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     protected void RaisePropertyChanged([CallerMemberName]string propertyName = "") 
     { 
      var handler = PropertyChanged; 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 

這是電網XAML代碼:

<DataGrid x:Name="dataGridFilters" CanUserAddRows="True" ItemsSource="{Binding FilterCriteriaList}" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="313,31,0,0" VerticalAlignment="Top" Height="261" Width="679"> 
      <DataGrid.Columns> 
       <DataGridTemplateColumn Header="Column"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <ComboBox x:Name="ComboBoxColumnName" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.SelectedTableColumnList}" 
             SelectedValue="{Binding Path=ColumnName,Mode =TwoWay,UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True,NotifyOnSourceUpdated=True}" SelectedValuePath="ColumnName"/> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
       <DataGridTemplateColumn Header="Operator"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <TextBox x:Name="TextBoxOperator" Text="{Binding Path=Operator,Mode =TwoWay,UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True,NotifyOnSourceUpdated=True}"/> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
       <DataGridTemplateColumn Header="Value"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <TextBox x:Name="TextBoxInputValue" Text="{Binding Path=InputValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True,NotifyOnSourceUpdated=True}"/> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
      </DataGrid.Columns> 
     </DataGrid> 
     <Button x:Name="buttonAddFilter" Content="+" HorizontalAlignment="Left" Margin="958,76,0,0" VerticalAlignment="Top" Width="34" RenderTransformOrigin="0.5,0.5" Click="buttonAddFilter_Click"> 
     </Button> 

點擊按鈕,我試圖在網格中添加該行。爲此,我在buttonAddFilter_Click事件中添加了viewModel.FilterCriteriaList.Add(new FilterCriteria());。此代碼作爲數據網格中的新行添加。但是當我在新增行中輸入一些值時。它不會被添加到FilterCriteriaList集合

請讓我知道,如何對新添加的行進行雙向綁定?

回答

0

在這裏,您在FilterCriteriaList集合中所做的更改不會調用它的setter。這就是原因,你的改變並不反映在datagrid中。爲了克服這個問題,您必須訂閱FilterCriteriaList的集合已更改事件。關於這方面的一個很好的討論可以在this SO post找到。通過它,讓我知道如果你需要更多的細節。