2012-06-05 12 views
5

cách thêm nút xóa cho mỗi hàng trong lưới dữ liệu (chứa hai trường) trong WPF. trong khi DataGrid itemsource làThêm nút xóa cho mỗi hàng trong datagrid

ObservableCollection<Result> 

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 
} 

}

Trả lời

21

Bạn có thể thêm một DataGridTemplateColumn và thêm một Button-CellTemplate của nó. Sau đó, sử dụng được xây dựng trong ApplicationCommands.Delete hoặc của riêng bạn ICommand cho 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> 

Cập nhật
Nếu bạn không muốn sử dụng được xây dựng trong Delete bạn có thể sử dụng riêng của bạn ICommand. Dưới đây là một ví dụ ngắn với RelayCommand có thể được tìm thấy trong liên kết sau: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx. Ngoài ra, tôi tải lên nó ở đây: 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> 

Và trong viewmodel hoặc mã của bạn đằng sau

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

nhờ cho việc cập nhật bằng cách thêm 'ICommand' cách xoá. – zulucoda

+0

@Fredrik Hedblad và cách xóa mục khỏi cơ sở dữ liệu .mdf bằng nút này 'Content = "Delete" Command = "Delete" ' –