如何从 Icommand 命令添加新的可观察集合

本文关键字:观察 集合 添加 Icommand 命令 | 更新日期: 2023-09-27 18:35:54

我有一个简单的MVVM项目,我正在学习。我正在尝试通过 ICommand 命令添加到可观察集合,但我无法?

主窗口.cs我没有添加任何内容*

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.DataContext>
        <Local:ViewModel></Local:ViewModel>
    </Grid.DataContext>
    <ListView Grid.Row="0" x:Name="lstNames" Margin="5,5,5,5" Grid.Column="1" ItemsSource="{Binding View_}">
        <ListView.View>
            <GridView x:Name="Setting_Items">
                <GridViewColumn Header="Setting_A"  DisplayMemberBinding="{Binding View_String}"/>
            </GridView>
        </ListView.View>
    </ListView>
   <TextBox Height="23" 
            HorizontalAlignment="Left" 
            Margin="145,195,0,0" 
            Name="textBox1" 
            VerticalAlignment="Top" 
            Width="120" />
  <ComboBox Height="23" 
            HorizontalAlignment="Left" 
            Margin="269,195,0,0" 
            Name="My_ComboBox" 
            VerticalAlignment="Top" 
            Width="222"     
            ItemsSource="{Binding View_}"/>
    <Button Content="Message Text" 
            Height="23" 
            HorizontalAlignment="Left"
            Margin="52,166,0,0" 
            Name="button1" 
            VerticalAlignment="Top" 
            Width="75" 
            CommandParameter="{Binding Text, ElementName=textBox1}"
            Command="{Binding Print_Line}"/>
    <Button Content="Add To Drop"
            Height="23" 
            HorizontalAlignment="Left"
            Margin="52,195,0,0" 
            Name="button2" 
            VerticalAlignment="Top" 
            Width="75" 
            />

</Grid>


    public class View
{
    public string View_String {get; set;}
}

    public class SimpleDelegateCommand : ICommand
{
    Action<object> _executeDelegate;
    public SimpleDelegateCommand(Action<object> executeDelegate)
    {
        _executeDelegate = executeDelegate;
    }
    public void Execute(object parameter)
    {
        _executeDelegate(parameter);
    }
    public bool CanExecute(object parameter) { return true; }
    public event EventHandler CanExecuteChanged;
}

 public class ViewModel
{
    private ObservableCollection<View> _View;
    public string _View_String { get; set; }
    public ObservableCollection<View> View_
    {
        get { return _View; }
        set { _View = value; }
    }
    ICommand _Print_Line = new SimpleDelegateCommand((x) => MessageBox.Show(x.ToString()));
    ICommand _Add_Line = new SimpleDelegateCommand((x) =>
         View_ = new ObservableCollection<View>() /////////Error HERE
        {
            new View(){View_String = x.ToString()}
        }
        );

    public ViewModel()
    {
        View_ = new ObservableCollection<View>()
        {
            new View(){View_String = "Setting 1"},
            new View(){View_String = "Setting 2"}
        };
    }
    public ICommand Print_Line { get { return _Print_Line; } }
    public ICommand Add_Line { get { return _Add_Line; } }
}

如何使用 ICommand 命令添加到我的 ObservableCollection?或者我该怎么做?

此外,如何使用 ICommand 命令执行多个任务,例如: ICommand _Print_Line = new SimpleDelegateCommand((x) => MessageBox.Show(x.ToString());MessageBox.Show("第二个任务"));

如何从 Icommand 命令添加新的可观察集合

执行多个任务:

_Print_Line = new SimpleDelegateCommand((x) => {
   MessageBox.Show(x.ToString());  
   MessageBox.Show("Second task");
 });

将私有集添加到命令的字段中,以便仅从类访问它

private ICommand print_Line;
public ICommand Print_Line { 
                             get { return print_Line; } 
                             private set { print_Line = value; } 
                           }
private ICommand add_Line;
public ICommand Add_Line  { 
                             get { return add_Line; } 
                             private set { add_Line = value; } 
                          }

也许也可以通过这种方式提供帮助:

private ICommand print_Line;
public ICommand Print_Line { get { return print_Line; } }
private ICommand add_Line;
public ICommand Add_Line{ get { return add_Line; } }

Commands初始化代码移动到ViewModel类的constructor内。您正在尝试访问尚未构建View_,因此出现错误。构造函数外部的对象初始值设定项不应调用实例方法/属性,因为在此之前不会构造类。

ICommand _Print_Line;
ICommand _Add_Line;

public ViewModel()
{
    _Print_Line = new SimpleDelegateCommand((x) => MessageBox.Show(x.ToString()));
    _Add_Line = new SimpleDelegateCommand((x) =>
     View_ = new ObservableCollection<View>() /////////Error HERE
    {
        new View(){View_String = x.ToString()}
    }
    );
    View_ = new ObservableCollection<View>()
    {
        new View(){View_String = "Setting 1"},
        new View(){View_String = "Setting 2"}
    };
}

首先,你的代码几乎不可读。错误非常简单。初始化字段时,不能引用非静态字段。ICommand _Add_Line是一个实例字段。就像_View一样。如果要引用它,请在类的构造函数中初始化 ICommand _Add_Line。每次调用非静态字段或方法时,都需要该类的实例来获取其值。关于这个问题,在stackoverflow上有几个答案