通过xaml绑定一个类集合属性

时间:2012-09-07 10:32:04

标签: c# wpf xaml data-binding itemsource

这是我的Globals课程

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

    protected Globals()
    {

    }

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

    public TrackList Tracklist = new TrackList();
}

这是智能代码中的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
        }
    }
}

我希望使用XAML绑定ListView中的Globals.Instance.Tracklist.Tracks。

通过运行时,使用ItemSource属性非常简单

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

但是使用xaml我尝试了几个代码,但没有一个是好的。

2 个答案:

答案 0 :(得分:0)

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

Tracklist必须是一个属性。将您的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; }
}

答案 1 :(得分:0)

在您查看模型中创建类型为Globals的Property,如下所示:

    property Globals Globals {get;set;}

在XAML中绑定它:

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