Tamaska Janos
Tamaska Janos

Reputation: 61

WPF binding Datasource is updated through reference

is it possible to set a Datasource through a reference?

public partial class GraphView : UserControl
{
    public ObservableCollection<ChartCollection<long>> signals { get; set; }

    public GraphView()
    {
       UCGraph.DataSource = this.signals;
    }
 }

and if I set the signals property should it update the Datasource?

MyGraphUC.signals = mySignals;

It doesn't seem to be working for me. Why?

Upvotes: 0

Views: 51

Answers (1)

Frank J
Frank J

Reputation: 1706

No you can't directly because the variables UCDataGraph.DataSource and signals are not connected by any means. They just happen to point to the same instance after you assign them in your constructor (actually they will both point to null which is not an instance at all). That being said, you can leverage the setter to do your bidding like so:

public partial class GraphView : UserControl
{
    private ObservableCollection<ChartCollection<long>> _signals 
    public ObservableCollection<ChartCollection<long>> signals 
    { 
        get
        {
            return _signals;
        }

        set
        {
            this._signals = value;
            UCGraph.DataSource = this._signals;
        }

    }

    public GraphView()
    {
       UCGraph.DataSource = this.signals;
    }
 }

Alternatively you can just .Clear() the observable collection and refill it with the elements instead of changeing the collection itself if that is an feasible scenario for you.

Upvotes: 1

Related Questions