Skip to main content
 首页 » 编程设计

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

2024年02月23日19myhome

我想将 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
(基本上你不能返回匿名类型 - 缺少这样的技巧 - 类型的“推断”是这背后的原因)。