将C#ObservableCollection示例转换为VB.NET数据集或等效项

时间:2011-02-23 19:19:07

标签: c# wpf vb.net binding

我有一个填充多选组合框的C#示例。这是从网上找到的一个例子。它可以与来自可观察集合的静态值一起使用。

我想将其更改为从SQL Server后端进行数据库驱动,但遇到了问题。我确实让它填充了组合框,但是选择的表现有些棘手。以下是任何人都可以提供帮助的示例代码。来自Northwind表的某种类型的简单查询就足够了,例如:“从类别中选择CategoryName”。

所以问题是:如何将此ObservableCollection转换为使用数据库中的查询而不是下面示例中使用的静态字符串列表?同时实现相同的属性改变了功能。

class DataSource : INotifyPropertyChanged
{

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion

    private ObservableCollection<string> _animals = new ObservableCollection<string> { "Cat", "Dog", "Bear", "Lion", "Mouse", "Horse", "Rat", "Elephant", "Kangaroo", "Lizard", "Snake", "Frog", "Fish", "Butterfly", "Human", "Cow", "Bumble Bee" };

    public ObservableCollection<string> Animals
    {
        get { return _animals; }
    }

    private string _selectedAnimal = "Cat";
    public string SelectedAnimal
    {
        get { return _selectedAnimal; }
        set
        {
            _selectedAnimal = value;
            OnPropertyChanged("SelectedAnimal");
        }
    }

    private ObservableCollection<string> _selectedAnimals;
    public ObservableCollection<string> SelectedAnimals
    {
        get
        {
            if (_selectedAnimals == null)
            {
                _selectedAnimals = new ObservableCollection<string> { "Dog", "Lion", "Lizard" };
                SelectedAnimalsText = WriteSelectedAnimalsString(_selectedAnimals);
                _selectedAnimals.CollectionChanged +=
                    (s, e) =>
                    {
                        SelectedAnimalsText = WriteSelectedAnimalsString(_selectedAnimals);
                        OnPropertyChanged("SelectedAnimals");
                    };
            }
            return _selectedAnimals;
        }
        set
        {
            _selectedAnimals = value;
        }
    }

    public string SelectedAnimalsText
    {
        get { return _selectedAnimalsText; }
        set
        {
            _selectedAnimalsText = value;
            OnPropertyChanged("SelectedAnimalsText");
        }
    } string _selectedAnimalsText;


    private static string WriteSelectedAnimalsString(IList<string> list)
    {
        if (list.Count == 0)
            return String.Empty;

        StringBuilder builder = new StringBuilder(list[0]);

        for (int i = 1; i < list.Count; i++)
        {
            builder.Append(", ");
            builder.Append(list[i]);
        }

        return builder.ToString();
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解这一点,但......

您是否考虑过将LINQ与数据实体一起使用?

将ADO.NET数据实体模型添加到项目中,并使用向导连接到您的数据源。

然后编写一个LINQ语句,从Entity对象中选择您想要的列并将其设置为ItemSource

示例:

myDataEntity Context = new myDataEntity();

List<Animals> myAnimals = new List<Animals>();
myAnimals = Context.Animals.ToList;

var myItemSource =
        from a in myAnimals
        select new { SelectedAnimal = a.animal, SelectedAnimalText = a.animaltext };

LINQ select语句创建一个匿名类型,如果设置为ItemSource,则包含两个字段作为列,即:SelectedAnimal和SelectedAnimalText

现在只需设置ComboBox的ItemSource属性

ComboBox.ItemsSource == myItemSource;

你已经完成了!