绑定与ListBox无法正常工作

时间:2015-12-22 23:45:07

标签: c# wpf mvvm binding listbox

我将自定义类的List(实现INotifyPropertyChanged)绑定到ListBox。

当我向List添加任何项目时,ListBox不会更新,但是如果我滚动,ListBox会更新。

班级

class MyClass : INotifyPropertyChanged
{
    private string name = string.Empty;
    public string Name { /* INotifyPropertyChanged */ }
}

属性

private List<MyClass> loaded;
public List<MyClass> Loaded { /* INorutyPropertyChanged */ }

ListBox

<ListBox ItemsSource={Binding Loaded} />

如果我强制覆盖List属性,它可以正常工作:

Loaded = new List<MyClass>() { new MyClass { Name = "test"; } }

1 个答案:

答案 0 :(得分:1)

更新至:

public ObservableCollection<MyClass> Loaded { get; private set; }

<ListBox ItemsSource={Binding Loaded, UpdateSourceTrigger=PropertyChanged} />

此外,您无需为INotiftyPropertyChanged媒体资源Loaded使用<Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <ListBox Width="200" ItemsSource="{Binding Items, UpdateSourceTrigger=PropertyChanged}"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Value}"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <Button Width="100" Height="75" HorizontalAlignment="Right" Content="Add" Command="{Binding AddItem}"/> </Grid> </Window> 。如果绑定发生一次,并且数据源没有改变,则不需要。

修改

这是一个有效的例子。

<强> MainWindow.xaml

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new DataContext();
    }
}

<强> MainWindow.xaml.cs

namespace WpfApplication3
{
    public class DataContext
    {
        public ObservableCollection<Item> Items { get; private set; }
        public ICommand AddItem { get; private set; }

        public DataContext()
        {
            Items = new ObservableCollection<Item>
            {
                new Item
                {
                    Value = "test"
                }
            };
            AddItem = new RelayCommand(() =>
            {
                Items.Add(new Item
                {
                    Value = "new item"
                });
            }, () => true);
        }
    }

    public class Item
    {
        public string Value { get; set; }
    }
}

<强> DataContext.cs

public class RelayCommand : ICommand
{
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
    private Action methodToExecute;
    private Func<bool> canExecuteEvaluator;
    public RelayCommand(Action methodToExecute, Func<bool> canExecuteEvaluator)
    {
        this.methodToExecute = methodToExecute;
        this.canExecuteEvaluator = canExecuteEvaluator;
    }
    public RelayCommand(Action methodToExecute)
        : this(methodToExecute, null)
    {
    }
    public bool CanExecute(object parameter)
    {
        if (this.canExecuteEvaluator == null)
        {
            return true;
        }
        else
        {
            bool result = this.canExecuteEvaluator.Invoke();
            return result;
        }
    }
    public void Execute(object parameter)
    {
        this.methodToExecute.Invoke();
    }
}

<强> RelayCommand.cs

db.users.aggregate({
    $group : {_id : "$age"}
    }
);

如果您有任何问题,请与我们联系。