使用依赖项属性绑定到UserControl中ListBox的ItemsSource

时间:2017-06-23 14:12:54

标签: wpf xaml data-binding

我正面临这个奇怪的问题。这看起来是众所周知的问题。我试图找到解决方案。花了一整天。但仍然没有用。我试过的所有解决方案都没有帮助我。

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

root是用户控件的名称,而ItemsSource是依赖项属性。

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

现在,在MainWindow.xaml中, 我创建了一个用户控件实例,并以这种方式绑定到Usercontrol的ItemsSource ..

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

其中windows是主窗口的名称

主窗口背后的代码是:

    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 });
    }

LineBarChartData类如下所示

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));
      }
    }
  }

列表框中没有显示任何内容。我无法找到我的错误。请帮忙!

感谢。

1 个答案:

答案 0 :(得分:2)

WPF数据绑定仅适用于公共属性,因此

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

应该是

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

您也可以删除支持字段并按以下方式编写属性(只读):

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

然后添加如下值:

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

注意,在绑定上设置UpdateSourceTrigger=PropertyChanged仅在具有TwoWay或OneWayToSource绑定时有效,并且Binding实际更新了源属性。在Bindings中不是这种情况。