ListBox数据绑定未正确更新

时间:2011-02-26 19:34:55

标签: c# wpf xaml data-binding observablecollection

我得到一个奇怪的结果,当绑定到ListBox的{​​{1}}绑定到ObservableCollection时,数据绑定在视图模型构造函数中,但是当我移动相同ObservableCollection时}人口代码到Button事件(尝试从视图后面的事件处理程序后面的代码和RoutedCommand)。没有async网络服务或后台主题。

当我单步执行代码时,从按钮事件调用时仍会填充视图模型中的ObservableCollection,但数据未在ListBox上更新。 XAML在两者中都是相同的。

以下是一些代码:

XAML

                <ListBox ItemsSource="{Binding Books}" BorderBrush="{x:Null}" Name="Results">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="{Binding Title}" FontSize="12" FontWeight="Bold" />
                            <TextBlock Text="{Binding ID}" FontSize="10" FontStyle="Italic" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
       ...
            <Button Command="{x:Static commands:BookQueryCommands.Query}" Content="Search" Width="119" Height="30" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,25,0,0"/>

查看背后的代码

public partial class BookExplorer : UserControl
{
    private BookExplorerViewModel vm;

    public BookExplorer()
    {
        this.InitializeComponent();

        vm = new BookExplorerViewModel();
        this.DataContext = vm;

        this.CommandBindings.Add(new CommandBinding(BookQueryCommands.Query, ExecuteQuery));
    }

    public void ExecuteQuery(object sender, ExecutedRoutedEventArgs e) {
        vm.ExecuteBookQuery();
    }
}

视图模型

    public class BookExplorerViewModel : DependencyObject
{   
    private IBookListProvider bookProvider;
    private bool canQuery = true;

    public ObservableCollection<BookModel> Books { get; set; }
    public string Title { get; set; }
    public string ID { get; set; }
    public DateTime LastModifiedDate { get; set; }

    public BookExplorerViewModel() {
        bookProvider = new BookListProvider();
    }

    public void ExecuteBookQuery() {
        Books = bookProvider.DocumentsQuery("temp");
    }
}

// bookProvider.DocumentsQuery("temp") just fills with dummy data.
// Binding works fine if the code in ExecuteBookQuery() is in the VM constructor
// Books is still populated fine in either case just binding is not updated

模型

public class BookModel : INotifyPropertyChanged
{
    private string title;
    public string Title {
        get { return title; }
        set {
            if (value != this.title) {
                this.title = value;
                NotifyPropertyChanged(Title);
            }
        }
    }

    private string id;
    public string ID {
        get { return id; }
        set {
            if (value != this.id) {
                this.id = value;
                NotifyPropertyChanged(ID);
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info) {
        if (PropertyChanged != null) {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

2 个答案:

答案 0 :(得分:3)

视图模型中的Books属性设置器不会触发PropertyChanged并且您绑定它,不是吗?将对象分配给Books属性时,View会绑定到该属性。当您为Books分配其他内容时,它是一个完全不同的对象,除非您触发一个事件,表明绑定应该重新连接到这个新集合,否则View不知道它。

答案 1 :(得分:0)

我将视图模型更改为:

public class BookExplorerViewModel : DependencyObject
{   
    private IBookListProvider bookProvider;
    private bool canQuery = true;

    public ObservableCollection<BookModel> Books { get; set; }
    public string Title { get; set; }
    public string ID { get; set; }
    public DateTime LastModifiedDate { get; set; }

    public BookExplorerViewModel() {
        bookProvider = new BookListProvider();            
        Books = new ObservableCollection<BookModel>(); 
    }

    public void ExecuteBookQuery() {
        Books.Clear();

        foreach(BookModel book in bookProvider.DocumentsQuery("temp")){
            Books.Add(book);
        }
    }
}

现在工作正常。

相关问题