【问题标题】:Attaching ObservableCollection as ItemsSource of ItemsControl将 ObservableCollection 附加为 ItemsControl 的 ItemsSource
【发布时间】:2013-10-01 10:45:17
【问题描述】:

我有一个像这样的对象:

class RCLocation : INotifyPropertyChanged
{
    private string _id;
    private string _name;
    private bool _checked;

    public string Id { /* get/set with NotifyPropertyChanged() */ }
    public string Name  { /* get/set with NotifyPropertyChanged() */ }
    public bool Checked { /* get/set with NotifyPropertyChanged() */ }

    /* INotifyPropertyChanged implementation methods */
}

现在在我的 MainWindow.xaml 中有一个 ItemsControl,如下所示:

<ItemsControl Name="lstDropOff" ScrollViewer.VerticalScrollBarVisibility="Auto">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding Checked, Mode=TwoWay}"/>
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我在后面的代码中将数据绑定到这个列表,如下所示:

ObservableCollection<RCLocation> dropOffs = new ObservableCollection<RCLocation>();
lstDropOff.ItemsSource = dropOffs;
dropOffs.Add(new RCLocation { /* some data here */ });
dropOffs.Add(new RCLocation { /* some data here */ });
dropOffs.Add(new RCLocation { /* some data here */ });
dropOffs.Add(new RCLocation { /* some data here */ });

我刚刚添加的项目没有显示在 ItemsControl 中。我到底做错了什么?想不通:/
感谢您的帮助。

【问题讨论】:

  • 你没有得到行来显示数据,或者你甚至没有得到行吗?
  • 我什至没有得到任何行,我在控件上有一个空的数据模板并且保留在那里,所以控件看不到任何项目...
  • 您还用 ItemsControl 做什么?我使用您提供的代码创建了一个干净的示例应用程序,它可以工作。您是否有机会尝试从后台/工作线程更新 ItemsSource?
  • 它现在正在工作,显然我只是很愚蠢,我在一个忘记调用的函数中创建绑定......

标签: c# wpf xaml data-binding observablecollection


【解决方案1】:

您尚未使用绑定设置ItemsSource,您需要执行此操作以涉及 WPF 绑定引擎并使控件对数据源更改做出反应。

下面是如何从代码隐藏中做到这一点:

// instead of lstDropOff.ItemsSource = dropOffs
var binding = new Binding() { Source = dropOffs };
lstDropOff.SetBinding(ItemsControl.ItemsSourceProperty, binding);

【讨论】:

  • 嗯是有道理的,但仍然没有得到任何行:/
  • 结果证明它有效,但我迟钝了,没有调用我创建绑定的函数:)
【解决方案2】:

您的代码没有问题,我尝试使用它...并显示在您的 xaml 代码中

using PhoneApp4.Resources;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Microsoft.Phone.Controls;

namespace PhoneApp4
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();

            ObservableCollection<RCLocation> dropOffs = new ObservableCollection<RCLocation>();
            lstDropOff.ItemsSource = dropOffs;
            dropOffs.Add(new RCLocation { Id = "1", Name = "Tester", Checked = true });
            dropOffs.Add(new RCLocation { Id = "1", Name = "Tester", Checked = true });
            dropOffs.Add(new RCLocation { Id = "1", Name = "Tester", Checked = true });
            dropOffs.Add(new RCLocation { Id = "1", Name = "Tester", Checked = true });
        }
    }

    class RCLocation 
    {
        private string _id;
        private string _name;
        private bool _checked;

        public string Id { get; set; }
        public string Name { get; set; }
        public bool Checked { get; set; }
    }
}

【讨论】:

    猜你喜欢
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-04
    相关资源
    最近更新 更多