简体   繁体   English

通过xaml绑定类集合属性

[英]Bind a class collection property via xaml

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

I wish to bind Globals.Instance.Tracklist.Tracks in a ListView using XAML. 我希望使用XAML将Globals.Instance.Tracklist.Tracks绑定到ListView中。

Via runtime, is really easy using ItemSource property 通过运行时,使用ItemSource属性非常容易

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

but using xaml I tried with several codes but none is good. 但是使用xaml我尝试了几种代码,但是都不好。

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

Tracklist has to be a property. 跟踪列表必须是一个属性。 Change your Globals class to: 将您的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; }
}

In you view model create Property with type Globals as follows: 在视图模型中,创建类型为Globals的Property,如下所示:

    property Globals Globals {get;set;}

In XAML bind to it: 在XAML中绑定到它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM