2012-09-07 61 views
0

綁定類集合屬性這是我的Globals類通過XAML

public class Globals 
{ 
    private static Globals instance = new Globals(); 

    protected Globals() 
    { 

    } 

    public static Globals Instance 
    {    
     get { return instance; } 
    } 

    public TrackList Tracklist = new TrackList(); 
} 

這是曲目在智能代碼:

public class TrackList : SettingAttribute { 
    public TrackList() 
    { 
     this.tracks = new ObservableCollectionExt<Track>(); 
    } 

    protected ObservableCollectionExt<Track> tracks; 

    public ObservableCollectionExt<Track> Tracks 
    { 
     get 
     { 
      return tracks; 
     } 
    } 

    public class Track : ICloneable 
    { 
     protected Track() 
     { 
      // Track instance is achieved by GetTrack 
     } 

     public GetTrack(string path) 
     { 
      // code implementation here 
     } 
    } 
} 

我希望綁定在Globals.Instance.Tracklist.Tracks ListView使用XAML。

通過運行時,是很容易使用的ItemSource屬性

lv.ItemsSource = Globals.Instance.Tracklist.Tracks; 

但使用XAML我與幾個代碼嘗試,但沒有一個是好的。

回答

0
ItemsSource="{Binding Tracklist.Tracks, Source={x:Static local:Globals.Instance}}" 

曲目列表必須是屬性。您Globals類更改爲:

public class Globals 
{ 
    private static Globals instance = new Globals(); 

    protected Globals() 
    { 
     Tracklist = new TrackList(); 
    } 

    public static Globals Instance 
    { 
     get { return instance; } 
    } 

    public TrackList Tracklist { get; private set; } 
} 
0

在您查看模型類型全局創建屬性如下:

property Globals Globals {get;set;} 

在XAML綁定到它:

<ListView ItemsSource="{Binding Path=Globals.Instance.Tracklist.Tracks}"> 
+0

Syned我follewed您的解決方案和有用! 也非常感謝所有的答案。 – user1649247

+0

歡迎您! – syned