当我从列表视图中选择项目并在WPF上的UI中单击添加按钮时,如何将项目添加到列表

时间:2017-05-12 10:11:06

标签: c# wpf listview mvvm relaycommand

我是新手,请原谅我的问题,如果它太褪色或者不清楚。 无论如何,在我的UI(WPF)中,我有一个我创建的ListView包含一个类型为Collection = new ObservableCollection<type>的可观察集合,我有两个按钮&#34;添加&#34; &安培; &#34;删除&#34;我想这样做:

1 - 每当我从UI中的ListView中选择一个项目(只需单击它),然后单击&#34;添加&#34;按钮,该项目存储在名为Scenario(Scenario = new List<type>)的列表中。

2-每当我点击&#34;删除&#34;按钮“场景”列表变为空。

我已经尝试了一些东西,但它没有像它应该的那样工作,我只能在列表情景中添加一个项目,然后在

中阻止(在调试时)

public bool CanExecute(object parameter) { return _canExecute == null || _canExecute(parameter); }

有人可以告诉我为什么吗?以及如何解决它? 至于&#34;删除&#34;按钮我还没有达到它,因为另一个没有正常工作。

如果您能为这个问题提出新的解决方案或解决方案,我会非常感激。

这是我迄今为止所做的。

这是MainWindowModel中的代码:

private ObservableCollection<Type> _collection,_scenario;
public MainWindowModel()
{
        Collection = new ObservableCollection<type>();
        Scenario=new ObservableCollection<Type>();
        DeleteCommand = new RelayCommand(o => DeleteExecute());
        AddTypeCommand = new RelayCommand(o => AddTypeExecute());

}
private Type _isSelected;
public Type IsSelected;
{
        get { return _isSelected;  }
        set
        {
            if (_isSelected != value)
            {
                _isSelected = value;
                RaisePropertyChanged(nameof(IsSelected));

            }
        }
}
public ICommand DeleteCommand
{
        get;
        private set;
}
private RelayCommand _addTypeCommand;
public ICommand AddTypeCommand
{
        get
        {
            if (_addTypeCommand == null)
            {
                _addTypeCommand = new RelayCommand(o => AddTypeExecute());
            }
            return  _addTypeCommand;
        }
        set { }
}

private void DeleteExecute()
{
        Scenario.Clear(); // Would this Work ?
}


private bool CanExecuteAddTypeCommand()
{
        return true;
}

private void AddTypeExecute()
{
        if (IsSelected != null)
        {

            Scenario.Add(IsSelected);

        }

}
public ObservableCollection<Type> collection
{
        get { return _collection; }
        set { SetPropertyAndFireEvent(ref _collection, value); }
}
public ObservableCollection<Type> Scenario
{
        get { return _scenario; }
        set { SetPropertyAndFireEvent(ref _scenario, value); }
}

与MainWindowModel

相同
<Window.DataContext>
    <viewModels:MainWindowModel />
</Window.DataContext>

<Grid>
  <ListView Grid.Row="2" 
                  Grid.Column="0"
                  ItemsSource="{Binding Collection}"
                  SelectedItem="{Binding IsSelected}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
  </ListView>

  <Button Command="{Binding AddTypeCommand}" 
                Width="100" 
                Height="100" 
                Content="Add" 
                Grid.Row="0" 
                Grid.Column="2"/>

  <Button Command="{Binding DeleteCommand}" 
                Content="Delete" 
                Width="100" 
                Height="100" 
                Grid.Row="2"
                Grid.Column="2" />
</Grid>

至于RelayCommand.cs

public class RelayCommand : ICommand
{
    private readonly Action<object> _execute;
    private readonly Func<object, bool> _canExecute;
    //Notifies the Button bounded to the ICommand that the value returned by CanExecute has changed 
    public event EventHandler CanExecuteChanged
    {
        //raised whenever the commandmanager thinks that something has changed that will affect the ability of commands to execute
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {

        return _canExecute == null || _canExecute(parameter);
    }

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

0 个答案:

没有答案