1
votes

I'm facing this weird problem.It looks like well known question. I tried to find the solution. It took one whole day. But still no use. All the solutions I tried didn't help me.

   <ListBox ItemsSource="{Binding ElementName=root,Path=ItemsSource,UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Category" Background="Coral"></ListBox>

root is the name of the User Control and ItemsSource is the dependency Property.

public static readonly DependencyProperty ItemsSourceProperty = 
  DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(LineBarChart));
public IEnumerable ItemsSource
{
  get { return (IEnumerable)GetValue(ItemsSourceProperty); }
  set {
    SetValue(ItemsSourceProperty, value); }
}

Now, In MainWindow.xaml, I created an instance of the User Control and Binded to the ItemsSource of Usercontrol this way..

 ItemsSource="{Binding ElementName=Window,Path=LineBarData,UpdateSourceTrigger=PropertyChanged}"

where windows is the name of the Main Window

The code behind of the main window is:

    public partial class MainWindow : Window
  {
    private ObservableCollection<LineBarChartData> lineBarData; 

    internal ObservableCollection<LineBarChartData> LineBarData
    {
      get
      {
        return lineBarData;
      }

      set
      {
        lineBarData = value;
      }
    }

    public MainWindow()
    {
      InitializeComponent();
      LineBarData = new ObservableCollection<LineBarChartData>();
      LineBarData.Add(new LineBarChartData() { Category = "January", ValueX = 10, ValueY = 50 });
      LineBarData.Add(new LineBarChartData() { Category = "February", ValueX = 20, ValueY = 60 });
      LineBarData.Add(new LineBarChartData() { Category = "March", ValueX = 30, ValueY = 70 });
      LineBarData.Add(new LineBarChartData() { Category = "April", ValueX = 40, ValueY = 80 });
    }

And the LineBarChartData class is like below

public class LineBarChartData : INotifyPropertyChanged
  {
    private string _category;
    public string Category
    {
      get { return this._category; }
      set { this._category = value; this.OnPropertyChanged("Category"); }
    }

    private double _valueX;
    public double ValueX
    {
      get { return this._valueX; }
      set { this._valueX = value; this.OnPropertyChanged("ValueX"); }
    }

    private double _valueY;
    public double ValueY
    {
      get { return this._valueY; }
      set { this._valueY= value; this.OnPropertyChanged("ValueY"); }
    }


    //private Brush _color;
    //public Brush Color
    //{
    //  get { return this._color; }
    //  set { this._color = value; this.OnPropertyChanged("Color"); }
    //}

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
      if (this.PropertyChanged != null)
      {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    }
  }

Nothing is being displayed in the listbox. I am unable to find my mistake. Please help!

Thanks.

1

1 Answers

2
votes

WPF data binding only works with public properties, so

internal ObservableCollection<LineBarChartData> LineBarData { get; set; }

should be

public ObservableCollection<LineBarChartData> LineBarData { get; set; }

You might also drop the backing field and write the property (as read only) like this:

public ObservableCollection<LineBarChartData> LineBarData { get; }
    = new ObservableCollection<LineBarChartData>();

Then add values like this:

public MainWindow()
{
    InitializeComponent();
    LineBarData.Add(new LineBarChartData() { ... });
    ...
}

As a note, setting UpdateSourceTrigger=PropertyChanged on a Binding only has an effect when it is a TwoWay or OneWayToSource binding, and the Binding actually updates the source property. This is not the case in your Bindings.