2012-06-05 13 views
5

come aggiungere un pulsante Elimina per ogni riga in una griglia di dati (contiene due campi) in WPF. mentre datagrid ItemSource èAggiungere il pulsante Elimina per ogni riga nel datagrid

ObservableCollection<Result> 

e

public class Result : INotifyPropertyChanged 
{ 
    public string FriendlyName { get; set; } 
    public string Id { get; set; } 

    public Result(string name, string id) 
    { 
     this.FriendlyName = name; 
     this.Id = id; 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    void OnPropertyChanged(string property) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(property)); 
     } 
    } 

    #endregion 
} 

}

risposta

21

è possibile aggiungere una DataGridTemplateColumn e aggiungere un Button alla sua CellTemplate. Quindi utilizzare il costruito nel ApplicationCommands.Delete o il proprio ICommand per la Button

<DataGrid ItemsSource="{Binding Results}" 
      AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="FriendlyName" 
          Binding="{Binding FriendlyName}"/> 
     <DataGridTextColumn Header="Id" 
          Binding="{Binding Id}"/> 
     <DataGridTemplateColumn Header="Delete"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <Button Content="Delete" 
          Command="Delete"/> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

Aggiornamento
Se non si desidera utilizzare il costruito nel Delete è possibile utilizzare il proprio ICommand. Ecco un breve esempio con RelayCommand che può essere trovato al seguente link: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx. Inoltre, ho caricato qui: RelayCommand.cs

<DataGrid ItemsSource="{Binding Results}" 
      SelectedItem="{Binding SelectedResult}" 
      AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
     <!-- ... --> 
     <DataGridTemplateColumn Header="Delete"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <Button Content="Delete" 
          Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, 
               Path=DataContext.DeleteCommand}" 
          CommandParameter="{Binding}"/> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

E nel tuo ViewModel o codice dietro

private Result m_selectedResult; 
public Result SelectedResult 
{ 
    get { return m_selectedResult;} 
    set 
    { 
     m_selectedResult = value;     
     OnPropertyChanged("SelectedResult"); 
    } 
} 

private bool CanDelete 
{ 
    get { return SelectedResult != null; } 
} 

private ICommand m_deleteCommand; 
public ICommand DeleteCommand 
{ 
    get 
    { 
     if (m_deleteCommand == null) 
     { 
      m_deleteCommand = new RelayCommand(param => Delete((Result)param), param => CanDelete); 
     } 
     return m_deleteCommand; 
    } 
} 

private void Delete(Result result) 
{ 
    Results.Remove(result); 
} 
+1

grazie per l'aggiornamento con l'aggiunta del modo in cui 'ICommand' di cancellazione. – zulucoda

+0

@Fredrik Hedblad e come eliminare elementi dal database .mdf utilizzando questo pulsante 'Contenuto =" Elimina " Comando =" Elimina "' –

Problemi correlati