[英]How to bind to a ViewModel Property from Application.Resources?
[英]bind list to object list property in xaml (wpf) (Application.Resources)
我一直在互聯網上搜索很多以解決此問題,但找不到合適的解決方案。 這是問題:(這是VS2010中的WPF應用程序)假設我有此類:
public class Airplane
{
public string AirplaneName{get;set;}
public ObservableCollection<People> myPeopleList {get;set;}
}
public class People
{
public string Name{get;set;}
public string SeatNumber{get;set;}
}
現在,我想在文件App.xaml中創建一個包含人員列表的Airplane類實例。 也就是說,創建我自己的資源(Application.Resources)。 我試過了,但出現異常:
<lcl:AirPlaneCollection x:Key="apCollection" >
<lcl:Airplane Name="airbus-780" myPeopleList="{StaticResource pList}" >
</lcl:AirPlaneCollection>
<lcl:PeopleCollection x:Key="pList">
<lcl:People Name="name1" SeatNumber="1"/>
<lcl:People Name="name2" SeatNumber="2"/>
</lcl:PeopleCollection>
當我嘗試使用該資源時,AirPlaneCollection和PeopleCollection只是從ObservableCollection<Airplane>
和ObservableCollection<Poeple>
繼承的簡單類(Application.Current.Resources [“ appCollection”]進行編譯,但在運行時出現異常。TreeView DataTemplate的性能很好,因為如果我將常規代碼列表中的類實例化為常規列表(添加等),它就可以正常工作..我在做什么錯?
終於找到了解決方案!! 在Application.Resources中,必須在“ apCollection”上方聲明“ pList”。 聲明順序可能很重要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.