2

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 Command Class`internal class RelayCommand : ICommand { #region フィールド

    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
}`
4

2 に答える 2

3

問題は、使用したバインディングが、DeleteCommand が EmployeeDetailsViewModel ではなく EmployeeModel のプロパティであると想定していることを意味することです。

おそらく、次のようなものが機能する可能性があります (概要のみ) - 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))
        }
    }
}
于 2012-06-20T12:02:09.213 に答える
1

以下のコードを使用して、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>

于 2012-06-20T13:53:17.053 に答える