Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WPF中使用DelegateCommand从列表中填充列表框?_C#_Wpf_Listbox_Viewmodel_Delegatecommand - Fatal编程技术网

C# 如何在WPF中使用DelegateCommand从列表中填充列表框?

C# 如何在WPF中使用DelegateCommand从列表中填充列表框?,c#,wpf,listbox,viewmodel,delegatecommand,C#,Wpf,Listbox,Viewmodel,Delegatecommand,我在填写列表框时遇到了一个问题。 我的xaml文件中有一个按钮: <Button Content="Show" Command="{Binding ShowSongsBy}" ... > 当我按下按钮时,会发生以下情况(同样在ViewModel中): 我看到了我希望在列表框中看到的内容:“1”、“2”和“Test1” 如果我只想列出“1”和“2”,我该怎么办 我的DelegateCommand类: class DelegateCommand : ICommand {

我在填写列表框时遇到了一个问题。 我的xaml文件中有一个按钮:

<Button Content="Show" Command="{Binding ShowSongsBy}" ... >
当我按下按钮时,会发生以下情况(同样在ViewModel中):

我看到了我希望在列表框中看到的内容:“1”、“2”和“Test1”

如果我只想列出“1”和“2”,我该怎么办

我的DelegateCommand类:

class DelegateCommand : ICommand
    {
        private Action<object> execute;
        private Func<object, bool> canExecute;

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public DelegateCommand(Action<object> execute, Func<object, bool> canExecute = null)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return this.canExecute == null || this.canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            this.execute(parameter);
        }
    }    
class DelegateCommand:ICommand
{
私人行动执行;
私人职能执行;
公共事件事件处理程序CanExecuteChanged
{
添加{CommandManager.RequerySuggested+=value;}
删除{CommandManager.RequerySuggested-=value;}
}
公共DelegateCommand(执行操作,Func canExecute=null)
{
this.execute=execute;
this.canExecute=canExecute;
}
公共布尔CanExecute(对象参数)
{
返回this.canExecute==null | | this.canExecute(参数);
}
public void Execute(对象参数)
{
执行(参数);
}
}    

谢谢。

使用
可观察收集
而不是
列表

private observeCollection\u sortedList;
公共可观察收集分类列表VM
{
获取=>\u排序列表;
设置
{
_sortedList=值;
onPropertyChanged();
}
}
。。。做你想用它做的事。与使用
列表
操作的方式相同

ListBox
将在您对集合进行任何更改时动态更新其布局

        private List<string> _sortedList;
        public List<string> SortedListVM
        {
            set
            {
                _sortedList = value;
                onPropertyChanged();
            }
            get { return _sortedList; }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        public void onPropertyChanged([CallerMemberName]string prop = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
        }
public ICommand ShowSongsBy
        {
            get
            {
                return new DelegateCommand((obj) =>
                {
                    SortedListVM = new List<string>();

                    List<string> testlist = new List<string>();
                    testlist.Add("1");
                    testlist.Add("2");

                    foreach (string i in testlist)
                    {
                        SortedListVM.Add(i);
                    }
                });
            }
        }
public ICommand ShowSongsBy
{
...
                    foreach (string i in testlist)
                    {
                        SortedListVM.Add(i);
                    }

                    SortedListVM.Add("Test1");
...
class DelegateCommand : ICommand
    {
        private Action<object> execute;
        private Func<object, bool> canExecute;

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public DelegateCommand(Action<object> execute, Func<object, bool> canExecute = null)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return this.canExecute == null || this.canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            this.execute(parameter);
        }
    }