Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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# “;使用ItemsSource之前,Items集合必须为空;错误wpf_C#_Wpf_Observablecollection - Fatal编程技术网

C# “;使用ItemsSource之前,Items集合必须为空;错误wpf

C# “;使用ItemsSource之前,Items集合必须为空;错误wpf,c#,wpf,observablecollection,C#,Wpf,Observablecollection,我试图绑定一个可观察的字符串集合。但当我启动一个应用程序时,我收到一个异常,即在使用ItemsSource之前,Items集合必须为空。当集合绑定时,集合中没有元素,那么会出现什么问题 我的Xaml <ListBox ItemsSource="{Binding Users}" Margin="10,77,805,228" Grid.RowSpan="2"> <ListBoxItem>

我试图绑定一个可观察的字符串集合。但当我启动一个应用程序时,我收到一个异常,即在使用ItemsSource之前,Items集合必须为空。当集合绑定时,集合中没有元素,那么会出现什么问题

我的Xaml

<ListBox ItemsSource="{Binding Users}"  Margin="10,77,805,228" Grid.RowSpan="2">
                    <ListBoxItem>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">

                            </StackPanel>
                        </DataTemplate>
                    </ListBoxItem>
                </ListBox>
<Button x:Name="AddUserButton" Content="Додати" Command="{Binding AddUserCommand}" RenderTransformOrigin="0.512,1.9"  />

我的视图模型(命令和可观察集合)

公共类UsersTabViewModel:ViewModelBase { 私有可观察收集用户; 私有字符串文本; 私有ICommand addUserCommand; 私人楼宇可执行; 公共用户stabViewModel() { _canExecute=true; 用户=新的ObservableCollection(); } 公共ObservableCollection用户{get;set;} 公共ICommand AddUserCommand { 得到 { 返回addUserCommand??(addUserCommand=newCommandHandler(()=>AddUserAction(),_canExecute)); } } 公共字符串文本 { 得到 { 返回文本; } 设置 { 文本=值; } } //文本绑定到此处 私有void AddUserAction() { 用户。添加(“收集”); } 公共类CommandHandler:ICommand { 私人行动; 私人楼宇可执行; 公共命令处理程序(操作,布尔canExecute) { _行动=行动; _canExecute=canExecute; } 公共布尔CanExecute(对象参数) { 返回-可执行; } 公共事件处理程序CanExecuteChanged; public void Execute(对象参数) { _动作(); } } 公共事件属性更改事件处理程序属性更改; }
正如错误试图告诉您的那样,如果您使用
ItemsSource
来绑定任何项目,您将无法拥有这些项目。
删除您的


要为绑定的项目设置模板,请设置

,因为错误试图告诉您,如果您使用
ItemsSource
绑定项目,您将无法拥有任何项目。
删除您的


要为绑定的项目设置模板,请设置

我修复了用项目清除列表框的问题。Clear()

我修复了用项目清除列表框的问题。Clear()

注意,您可以使用
复合收集
来充分利用这两个世界(大部分)。请注意,您可以使用
复合收集
来充分利用这两个方面(大部分)。
public class UsersTabViewModel : ViewModelBase
{
    private ObservableCollection<string> users;
    private string text;

    private ICommand addUserCommand;

    private bool _canExecute;

    public UsersTabViewModel() 
    {
        _canExecute = true;
        Users = new ObservableCollection<string>();
    }

    public ObservableCollection<string> Users { get; set; }


    public ICommand AddUserCommand
    {
        get
        {
            return addUserCommand ?? (addUserCommand = new CommandHandler(() => AddUserAction(), _canExecute)); 
        }

    }

    public string Text
    {
        get
        {
            return text;
        }

        set
        {
            text = value;
        }
    } 

    //text is bound to here
    private void AddUserAction()
    {

        Users.Add("collection");


    }

    public class CommandHandler : ICommand
    {
        private Action _action;
        private bool _canExecute;
        public CommandHandler(Action action, bool canExecute)
        {
            _action = action;
            _canExecute = canExecute;
        }

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

        public event EventHandler CanExecuteChanged;

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

    public event PropertyChangedEventHandler PropertyChanged;
}