我的对象是我要插入ObservableCollection
:
public ObservableCollection<MyData> collection{ get; set; }
this.DataContext = this;
collection= new ObservableCollection<MyData>();
现在我有Array
我的对象:
MyData[] array...
所以我在这里尝试将1个对象添加到ObservableCollection
collection.Add(array[0]);
并收到targetinvocationexception
例外
未处理的类型异常 &#39; System.Reflection.TargetInvocationException&#39;发生在 PresentationFramework.dll
答案 0 :(得分:0)
如果在调用InitializeComponent方法之前关联DataContext,通常会出现此错误。 例如
public MainWindow() {
timerViewModel = new TimerViewModel(new MyDateTime());
timerLabel.DataContext = timerViewModel; //error
InitializeComponent();
}
public MainWindow() {
timerViewModel = new TimerViewModel(new MyDateTime());
InitializeComponent();
timerLabel.DataContext = timerViewModel; //Ok
}
答案 1 :(得分:-1)
试一试: (我的答案是这个方法):
在外部范围内,声明public:
dfs = ()
for file in myFiles:
df = pd.read_csv(file, index_col='lang')
dfs = dfs + tuple((df,))
在Contructor中:
df = pd.concat(dfs,axis=0)
df = df.groupby(df.index).mean()
MyDataArray是:
public ObservableCollection<MyData> collection{ get; set; }
在添加到列表的按钮中:
this.DataContext = this;
collection= new ObservableCollection<MyData>();
MyData类是:
MyData[] array =
{
new MyData { MyProp = "A" },
new MyData { MyProp = "B" },
new MyData { MyProp = "C" }
};