2012-06-05 19 views
5

WPFでデータグリッド(2つのフィールドを含む)のすべての行に削除ボタンを追加する方法。データグリッドの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を追加し、そのCellTemplateButtonを追加することができます。そして、あなたはDeleteに建て使用したくない場合は、あなた自身のICommandを使用することができます
更新ApplicationCommands.Deleteまたは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> 

ための独自の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> 

をおよび削除の `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

感謝の後ろにあなたのviewmodelやコードで:また、私はここでそれをアップロードしました。 – zulucoda

+0

@Fredrik Hedbladと、このボタンを使って.mdfデータベースからアイテムを削除する方法 'Content =" Delete " Command =" Delete " –

関連する問題