2012-06-05 20 views
5

में प्रत्येक पंक्ति के लिए हटाएं बटन जोड़ें WPF में डेटा ग्रिड (दो फ़ील्ड वाले) में प्रत्येक पंक्ति के लिए डिलीट बटन कैसे जोड़ें। जबकि datagrid ItemSourceडेटाग्रिड

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 
} 

}

उत्तर

21

है आप एक DataGridTemplateColumn जोड़ सकते हैं और अपने CellTemplate करने के लिए एक Button जोड़ सकते हैं। तो फिर या 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> 

अद्यतन
के लिए ApplicationCommands.Delete में बनाया अपनी खुद की ICommand आप Delete में बनाया का उपयोग करने के आप अपने खुद के ICommand उपयोग कर सकते हैं नहीं करना चाहते हैं का उपयोग करें। यहां RelayCommand के साथ एक छोटा उदाहरण दिया गया है जो निम्न लिंक में पाया जा सकता है: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx। इसके अलावा, मैं इसे यहाँ अपलोड किया गया: 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> 

और अपने viewmodel या पीछे हटाने की `ICommand` तरह से जोड़कर अद्यतन करने के लिए

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

धन्यवाद कोड में। – zulucoda

+0

@Fredrik हेडब्लैड, और इस बटन का उपयोग कर .mdf डेटाबेस से आइटम को कैसे हटाएं 'सामग्री = "हटाएं" कमांड = "हटाएं" –