2012-06-20 56 views
1

我用數據模板按鈕綁定了一些記錄,用於刪除WPF數據網格中所選行的記錄。那麼我的問題是,刪除命令不起作用。我真的不知道爲什麼它沒有工作,儘管把每件事情都對了。請查看代碼並告訴我我做錯了什麼。你的小小努力將使我的工作完成。 感謝從數據網格中刪除命令不起作用

<Window x:Class="DemoMVVM.View.EmployeeDetails" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:viewModel="clr-namespace:DemoMVVM.ViewModel" 
    Title="EmployeeDetails" Height="200" Width="500"> 
<Window.DataContext> 
    <viewModel:EmployeeDetailsViewModel></viewModel:EmployeeDetailsViewModel> 
</Window.DataContext> 
<Grid> 
    <DataGrid AutoGenerateColumns="False" CanUserAddRows="False" ItemsSource="{Binding EmpDetailsList}" Grid.Column="0" SelectedItem="{Binding SelectedRecord,Mode=TwoWay}" Margin="10,17,10,8" > 

     <DataGrid.Columns> 
      <DataGridTextColumn Binding="{Binding Path=Id}" IsReadOnly="True" Visibility="Collapsed" Width="90"></DataGridTextColumn> 
      <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" IsReadOnly="True" Width="*"></DataGridTextColumn> 
      <DataGridTextColumn Header="Age" Binding="{Binding Path=Age}" IsReadOnly="True" Width="*"></DataGridTextColumn> 
      <DataGridTextColumn Header="City" Binding="{Binding Path=City}" IsReadOnly="True" Width="*"></DataGridTextColumn>    
      <DataGridTemplateColumn Header="Delete" Width="60" IsReadOnly="True"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <Button Content="Delete" Command="{Binding Path=DeleteCommand}" ></Button> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn>    
     </DataGrid.Columns> 
    </DataGrid> 
</Grid> 

public class EmployeeDetailsViewModel 
{ 
    private ObservableCollection<EmployeeModel> _empDetailsList; 
    private EmployeeModel _selectedEmp; 
    private RelayCommand _deleteCommand; 

    public EmployeeDetailsViewModel() 
    { 
     _empDetailsList = new ObservableCollection<EmployeeModel>() 
     { 
      new EmployeeModel(){Id=1,Name="ABC",Age=26,City="Las Vegas"}, 
      new EmployeeModel(){Id=2,Name="MNO",Age=27,City="New Delhi"}, 
      new EmployeeModel(){Id=3,Name="XYZ",Age=25,City="Sydney"}, 
     }; 
    } 

    public ObservableCollection<EmployeeModel> EmpDetailsList 
    { 
     get { return _empDetailsList; } 
    } 

    public ICommand DeleteCommand 
    { 
     get 
     { 
      if (_deleteCommand == null) 
       _deleteCommand = new RelayCommand(Delete); 
      return _deleteCommand; 
     } 
    } 

    private void Delete() 
    { 
    } 
} 

relay命令Class`internal類RelayCommand:ICommand的 { #地區字段

readonly Action _execute; 
    readonly Func<bool> _canExecute; 

    #endregion 

    #region Constructors 


    public RelayCommand(Action execute) 
     : this(execute, null) 
    { 
    } 

    public RelayCommand(Action execute, Func<bool> canExecute) 
    { 
     if (execute == null) 
      throw new ArgumentNullException("execute"); 

     _execute = execute; 
     _canExecute = canExecute; 
    } 

    #endregion // Constructors 

    #region ICommand Members 

    [DebuggerStepThrough] 
    public bool CanExecute(object parameter) 
    { 
     return _canExecute == null ? true : _canExecute(); 
    } 

    public event EventHandler CanExecuteChanged 
    { 
     add 
     { 
      if (_canExecute != null) 
       CommandManager.RequerySuggested += value; 
     } 
     remove 
     { 
      if (_canExecute != null) 
       CommandManager.RequerySuggested -= value; 
     } 
    } 

    public void Execute(object parameter) 
    { 
     _execute(); 
    } 

    #endregion // ICommand Members 
}` 
+0

你期待你的刪除命令做什麼?方法聲明是空的。 –

+0

@Benjamin,實際上我會添加業務邏輯來刪除該行。 – user1399377

回答

0

您可以使用下面的代碼它只是解決您的錯誤在XAML ......我剛纔提出你的XAML中的微小變化來解決這個..

 <DataGrid.Columns> 
      <DataGridTextColumn Binding="{Binding Path=Id}" IsReadOnly="True" Visibility="Collapsed" Width="90"></DataGridTextColumn> 
      <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" IsReadOnly="True" Width="*"></DataGridTextColumn> 
      <DataGridTextColumn Header="Age" Binding="{Binding Path=Age}" IsReadOnly="True" Width="*"></DataGridTextColumn> 
      <DataGridTextColumn Header="City" Binding="{Binding Path=City}" IsReadOnly="True" Width="*"></DataGridTextColumn>    
      <DataGridTemplateColumn Header="Delete" Width="60" IsReadOnly="True"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <Button Content="Delete" Command="{Binding Path=DataContext.DeleteCommand}" ></Button> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn>    
     </DataGrid.Columns> 
    </DataGrid> 
</Grid> 

3

的問題是,綁定你用過意味着它期望DeleteCommand是EmployeeModel而不是EmployeeDetailsViewModel的一個屬性。

也許像下面這樣可能工作(只是一個大綱) - 假設ListPresenter需要做從列表中刪除,並知道要刪除的項目:

public class EmployeeDetailsListPresenter : INotifyPropertyChanged 
{ 
    _empDetailsList = new ObservableCollection<EmployeeDetailsPresenter> 
     { 
      new EmployeeDetailsPresenter(new EmployeeModel(...), Delete), 
      ... 
     }; 

    public IEnumerable<EmployeeDetailsPresenter> EmpDetailsList { ... } 

    private void Delete(EmployeeDetailsPresenter employeeDetails) 
    { 
     _empDetailsList.Remove(employeeDetails); 
    } 
} 

public class EmployeeDetailsPresenter : INotifyPropertyChanged 
{ 
    public EmployeeDetailsPresenter(EmployeeModel employee, 
            Action<EmployeeDetailsPresenter> delete) 
    { 
     _employee = employee; 
     _delete = delete; 
    } 

    public ICommand DeleteCommand 
    { 
     get 
     { 
      return new RelayCommand(() => _delete(this)) 
     } 
    } 
} 
+0

我讀過的命令應該是視圖模型的一部分,在我的情況下,我的視圖模型是EmployeeDetailsViewModel。 – user1399377

+0

這是真的。但是,您只有員工細節列表的視圖模型,而不是員工細節本身的視圖模型。您應該將當前視圖模型重命名爲EmployeeDetailsListViewModel,併爲EmployeeDetailsViewModel添加一個新模型。 – Scroog1

+0

我同意Scroog1,DeleteCommand應該與Age,Name,City ...處於同一級別,即在EmployeeModel的ViewModel中。 –