可以說我有一個列表框如何轉換列表<T>列出<dynamic> WPF
<ListBox ItemsSource="{Binding MyList}"/>
我有一個屬性:
private List<dynamic> _myList;
public List<dynamic> MyList
{
get { return _myList; }
set
{
if(value==null)return;
_myList = value;
OnPropertyChanged();
}
}
好了,現在我們正在向我的問題: 我有幾個類,我將有列表,我希望能夠在這個ListBox中顯示它們。
類:
public class A
{
public string S { get; set; }
public int I { get; set; }
}
public class B
{
public int C { get; set; }
public string Name { get; set; }
public string F { get; set; }
}
public class C
{
public int I { get; set; }
public string Name { get; set; }
public string This { get; set; }
public double That { get; set; }
}
灌裝myList中
private void FillA()
{
List<A> listA = GetListAFromSomewhere();
MyList = listA;
}
private void FillB()
{
List<B> listb = GetListBFromSomewhere();
MyList = listb;
}
private void FillC()
{
List<C> listC = GetListCFromSomewhere();
MyList = listC;
}
我得到異常
無法隱式轉換類型 「System.Collections.Generic。 List'至 'System.Collections.Generic.List'
我試圖投射,並尋找一個轉換器。但似乎無法弄清楚。 當我更改爲List<object>
時,情況也是如此。 this
感謝您的幫助!