0

This is my Globals class

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

    protected Globals()
    {

    }

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

    public TrackList Tracklist = new TrackList();
}

This is TrackList in a smart code:

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
        }
    }
}

I wish to bind Globals.Instance.Tracklist.Tracks in a ListView using XAML.

Via runtime, is really easy using ItemSource property

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

but using xaml I tried with several codes but none is good.

H.B.
  • 166,899
  • 29
  • 327
  • 400
user1649247
  • 47
  • 2
  • 7

2 Answers2

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

Tracklist has to be a property. Change your Globals class to:

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; }
}
LPL
  • 16,827
  • 6
  • 51
  • 95
0

In you view model create Property with type Globals as follows:

    property Globals Globals {get;set;}

In XAML bind to it:

    <ListView ItemsSource="{Binding Path=Globals.Instance.Tracklist.Tracks}">
syned
  • 2,201
  • 19
  • 22
  • Syned I follewed your solution and it works! Thanks a lot for all the answers too. – user1649247 Sep 07 '12 at 11:56
  • @user1649247 Since the suggestion from user `@syned` worked for you, you may want to mark it as an `Answer`. It will help other readers of this post, as well. – nam Nov 04 '20 at 02:42