本文介绍了如何从Icommand命令添加新的ObservableCollection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在学习的简单MVVM项目.我正在尝试通过ICommand命令添加到ObservableCollection,但是我无法这样做?

I have a simple MVVM project I am learning on. I am trying to add to an ObservableCollection through an ICommand command, but I am unable to?

MainWindow.cs我没有添加任何内容*

MainWindow.cs I haven't added anything*

<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中?或我该怎么做?

How do I go about adding to my ObservableCollection using an ICommand command? or how do I go about it?

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

Also, how do you perform multiple tasks using an ICommand command, for example: ICommand _Print_Line = new SimpleDelegateCommand((x) => MessageBox.Show(x.ToString()); MessageBox.Show("Second task"));

推荐答案

执行多个任务:

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

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

Add private set to your command's field, for access it just from your class

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; } 
                          }

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

maybe can help this way too:

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

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

这篇关于如何从Icommand命令添加新的ObservableCollection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 12:55