使用依赖项属性在USERCORTROL中与ListBox的项目汇合



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

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

root是用户控制的名称,而项目源是依赖项属性。

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="{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));
      }
    }
  }

在列表框中没有显示任何内容。我找不到我的错误。请帮助!

谢谢。

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绑定时具有效果,并且绑定实际上更新了源属性。在您的绑定中并非如此。

最新更新