【问题标题】:LINQ to ComboBox XAMLLINQ 到组合框 XAML
【发布时间】:2013-12-09 13:54:23
【问题描述】:

我正在尝试将一些数据绑定到 ComboBox。我查了一些资料,但到目前为止我找到的所有解决方案都不尽如人意。我的目标是创建一个 LINQ 查询来选择我想要的所有必需数据。之后,我想用数据填充我的 ComboBox。

所以,我的查询应该是这样的:

var result = from p in db.PriorityTypes select new { Id = p.Id, Name = p.Name };

在我的组合框中,我希望有 SomePropery={Binding data} 之类的东西。这可能吗?我正在使用 WPF 技术。

注意 因此,ComboBox 应该是字符串的下拉列表,值应该是它们的 ID。

【问题讨论】:

    标签: .net wpf linq combobox


    【解决方案1】:

    ComboBox 为此使用 ItemsSource 属性:

    <ComboBox ItemsSource="{Binding Items}" DisplayMemberPath="Name" ValuePath="Id" />
    

    代码隐藏/查看模型:

    // You need to implement the `INotifyPropertyChanged` interface properly here:
    public ObservableCollection<YourDataType> Items { get; set; }
    

    更新ComboBox 项目就这么简单:

    var result = from p in db.PriorityTypes select new YourDataType(p.Id, p.Name);
    Items = new ObservableCollection<YourDataType>(result);
    

    如果您已经正确实现了INotifyPropertyChanged 接口并定义了一个数据类型类(以替换上面的YourDataType 条目),那么这就是您所需要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多