c# - 制作或声明 ObservableCollection<a> 全局变量,其中 a=匿名类型

标签 c# observablecollection datacontext

我想将 ListView 数据上下文设置为一个可观察的集合,以便对集合的更改可以反射(reflect)在我的 ListView 上。我将可观察集合创建为:

public static ObservableCollection<T> ToObservableCollection<T>(IEnumerable<T> enumeration)
{
     return new ObservableCollection<T>(enumeration);
}

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{

           Entities.DatabaseModel m = new Entities.DatabaseModel();
           var q = from t in m.TimeSheet                            
                    join emp in m.Employees on t.idEmployee equals emp.id
                    where emp.id == CurrentEmploye.id
                    select new
                    {
                        firstName = emp.firstName,
                        lastName = emp.lastName,
                        position = emp.position,
                        clockInDate = t.clockInDate,
                        clockOutDate = t.clockOutDate,
                    };
           // here I create the observablecollection!!!!!!!!!!!!!!
           listView1.DataContext = ToObservableCollection(q); 
}

现在我的问题是,如果我想将项目添加到 ObservableCollection,我该怎么做?如果我执行 listView1.DataContext.Add( 这将导致错误。

也就是说我有方法

    private void btnClockIn_Click(object sender, RoutedEventArgs e)
    {
         // I will like to add items to the observable collection in here

这是我试过的,但没有用:

    dynamic collection; 


private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
       // CurrentEmploye some employee
       Entities.DatabaseModel m = new Entities.DatabaseModel();
       var q = from t in m.TimeSheet                            
                join emp in m.Employees on t.idEmployee equals emp.id
                where emp.id == CurrentEmploye.id
                select new
                {
                    firstName = emp.firstName,
                    lastName = emp.lastName,
                    position = emp.position,
                    clockInDate = t.clockInDate,
                    clockOutDate = t.clockOutDate,
                };

       collection = ToObservableCollection(q); 
 }


    private void btnClockIn_Click(object sender, RoutedEventArgs e)
    {

         collection.Add(new
                {
                    firstName = "Antonio",
                    lastName = "Nam",
                    position = "Amin",
                    clockInDate = DateTime.Now,
                    clockOutDate = DateTime.Now
                });

最佳答案

您可以通过使用类似的"template"匿名变量来“欺骗”它...
例如代码方法就像...

static ObservableCollection<T> CastToEnumerable<T>(this T template, object value)
{
    return (ObservableCollection<T>)value;
}

...然后像这样使用它...

var list = new
{
    firstName = "",
    lastName = "",
    position = "",
    clockInDate = DateTime.MinValue,
    clockOutDate = DateTime.MinValue
}.CastToEnumerable(context);
foreach (var value in list)
{ 
}

...仍然建议为您的目的使用一个普通的命名类。
您可以在此处找到有关类似问题的更多信息...
Cast to Anonymous Type
(基本上你不能返回匿名类型 - 缺少这样的技巧 - 类型的“推断”是这背后的原因)。

关于c# - 制作或声明 ObservableCollection<a> 全局变量,其中 a=匿名类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9985057/

相关文章:

c# - 将集合绑定(bind)到 ContextMenu 子菜单

c# - 将 ObservableCollection 绑定(bind)为不同 CollectionViewSource 的源

sql-server - LINQ to SQL - Xml 字段问题 - 你能拦截生成的 SQL 有多低?

c# - 重构我的代码 : Conditions based on different variables

c# - 如何使用 Handle 中的协程 - Caliburn Micro 中 IResult 的实现

c# - 当列表中项目的属性更改时,ListCollectionView 上的 LiveFiltering 不会重新评估 Filter

list - Dart可观察 list 已移除项目

WPF 列表框 dataContextChanged

generics - 如何将d:DesignInstance设置为通用类型?

c# - 企业图书馆记录应用程序 block 选项