【问题标题】:MVVM WPF ObservableCollection : Error addign items, ItemsSource bindingMVVM WPF ObservableCollection:错误添加项目,ItemsSource 绑定
【发布时间】:2013-10-02 05:46:50
【问题描述】:

在使用带有 ItemsSource 的列表框时,我无法将项目添加到 ObservableCollection。我在我的视图模型构造函数中添加了用于测试的虚拟数据。

我的视图模型:

public class KabaDeviceListViewModel : KabaBase
{

    private ObservableCollection<KabaDeviceDetailViewModel> _details;

    public ObservableCollection<KabaDeviceDetailViewModel> KabaDevices
    {
        get { return _details; }
        set 
        {
            if (value != _details)
            {
                _details = value;
                OnPropertyChanged("KabaDevices");
            }
        }
    }


    public KabaDeviceListViewModel()
    {

        ObservableCollection<KabaDeviceDetailViewModel> _details = new ObservableCollection<KabaDeviceDetailViewModel>();

        KabaDevice kd1 = new KabaDevice("localhost A", "127.0.0.1", true);
        KabaDeviceDetailViewModel dvm = new KabaDeviceDetailViewModel(kd1);
        _details.Add(dvm);

        KabaDevice kd2 = new KabaDevice("localhost B", "127.0.0.1", true);
        KabaDeviceDetailViewModel dvm2 = new KabaDeviceDetailViewModel(kd2);
        _details.Add(dvm2);

        this.KabaDevices = _details;
    }
}

到目前为止一切顺利,但在 XAML 代码中出现错误,在列表框的 ItemsSource 上。我看不出我做错了什么。我使用 VS2010 和 .NET 4.0。

<UserControl x:Class="KabaTest.View.KabaDeviceListView"
         ...
         xmlns:myViewModels="clr-namespace:KabaTest.ViewModel"
         xmlns:myViews="clr-namespace:KabaTest.View">
<UserControl.DataContext>
    <myViewModels:KabaDeviceListViewModel/>
</UserControl.DataContext>
<Grid>
    <ListBox Margin="5" 
             ItemsSource="{Binding Path=KabaDevices, Mode=TwoWay}" >
             ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True" Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate DataType="{x:Type myViewModels:KabaDeviceDetailViewModel}" >
                <myViews:KabaDeviceDetailView DataContext="{Binding }"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

ItemsSource 的 InnerException 是:{“使用 ItemsSource 时操作无效。改为使用 ItemsControl.ItemsSource 访问和修改元素。”}。感谢您的帮助!

【问题讨论】:

  • 您的应用程序中是否有任何其他代码尝试直接在 ListBox 中添加/删除项目,而不是使用 KabaDevices 属性?

标签: wpf xaml mvvm listbox itemssource


【解决方案1】:

你的构造函数可能有问题。您将支持字段 _details 分配给该支持字段 KabaDevices 的公共属性。不是 100% 确定这是否是异常的原因,但据我所知,其他一切都应该正常工作。试试这个:

public KabaDeviceListViewModel()
{

    var details = new ObservableCollection<KabaDeviceDetailViewModel>();

    KabaDevice kd1 = new KabaDevice("localhost A", "127.0.0.1", true);
    KabaDeviceDetailViewModel dvm = new KabaDeviceDetailViewModel(kd1);
    details.Add(dvm);

    KabaDevice kd2 = new KabaDevice("localhost B", "127.0.0.1", true);
    KabaDeviceDetailViewModel dvm2 = new KabaDeviceDetailViewModel(kd2);
    details.Add(dvm2);

    this.KabaDevices = details;
}

【讨论】:

  • 正如您所暗示的,这不是错误的原因。 this.KabaDevices = _details;在@user2135342 代码中是无效的,因为 if (value != _details) 永远不会是真的。我怀疑真正的错误出在我们看不到的代码中。不过,您的代码更改建议是正确的!
  • 嗨,马克,感谢您的快速答复。我已经尝试过了,不幸的是这并不能解决问题。在构造函数中添加视图模型工作得很好......但不知何故它与 ItemsSource 的使用不兼容?再次感谢!
  • 大家好,确实,幕后有问题(在上面的代码显示之外)。我不记得我究竟做了什么改变,但我已经在需要的地方考虑了你的评论。这样,我认为您的回答很有帮助。多谢!最好的问候。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
相关资源
最近更新 更多