nrofis
nrofis

Reputation: 9796

WPF - Binding to dll function

How can I bind ListBox that show all items in ObservableCollection that return by a function in some dll?

I have in the dll singltone class called FilesManager and a function Instance() that return the pointer to this class. Then I have function that called GetFiles(), its returns ObservableCollection that contains all the files names.

And I have a ListBox in xml and I want to bind it ItemsSource property to
FilesManager.Instance().GetFiles(), How can I do that?

Upvotes: 1

Views: 966

Answers (1)

devdigital
devdigital

Reputation: 34359

You should consider using the MVVM design pattern. In this case, you would have a property on your view model that exposes the FilesManager.Instance().GetFiles() collection, and your view would bind to this property.

public class MyViewModel
{
    public MyViewModel()
    {
        this.Files = FilesManager.Instance().GetFiles();
    }

    public XXX Files { get; private set; }
}

<ListBox ItemsSource="{Binding Files}" ... />

If you wanted to change the Files reference after construction, you would need to implement INotifyPropertyChanged to update the UI.

Upvotes: 2

Related Questions