【问题标题】:Datagrid to ObservableCollection Binding Doesn't Update gridDatagrid 到 ObservableCollection 绑定不更新网格
【发布时间】:2012-03-21 12:17:45
【问题描述】:

请帮帮我,我不知道出了什么问题。无论我尝试什么,网格都没有更新(保持为空)。

我希望网格绑定到 ObservableCollection,但不是生成自动 cloumns,而是从名为 Product 的对象中选择两个 Properties,这是该 Collection 所拥有的类型。

XAML:

 <DataGrid  x:Name="itemsGrid" ItemsSource="{Binding OrdersList}" AutoGenerateColumns="False" Style="{StaticResource GridStyle}">
     <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=Product.Amount}" Header="AMOUTN" />
        <DataGridTextColumn Binding="{Binding Path=Product.Name}" Header="NAME"  />
     </DataGrid.Columns>
</DataGrid >

代码:

  public partial class Orders : Window,INotifyPropertyChanged
    {

      ObservableCollection<Product> _ordersList = new ObservableCollection<Product>();
      public event PropertyChangedEventHandler PropertyChanged;

      private ObservableCollection<Product> OrdersList
    {
        get { return this._ordersList; }

        set { _ordersList = value; NotifyPropertyChanged("OrdersList"); }
    }

    private void addProduct(Product p)
    {
        OrdersList.Add(p);
        NotifyPropertyChanged("OrdersList");
    }
    private void removeProduct(Product p)
    {
        OrdersList.Remove(p);
        NotifyPropertyChanged("OrdersList");
    }

    protected void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

}

【问题讨论】:

    标签: c# wpf binding grid observablecollection


    【解决方案1】:

    我认为您只需从绑定中删除单词Product.。每个DataGridRowDataContextProduct 类型的对象,因此您的绑定应该指向Product 上的属性

     <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=Amount}" Header="AMOUNT" />
        <DataGridTextColumn Binding="{Binding Path=Name}" Header="NAME"  />
     </DataGrid.Columns>
    

    【讨论】:

    • @meyou 尝试将 ItemsGrid.DataContext = this; 添加到 Window 的构造函数中。听起来好像没有 DataContext 设置。您还可以在 ItemsSource 上使用 RelativeSource 绑定:ItemsSource="{Binding OrdersList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 告诉绑定从哪里获取集合。哦,让 OrdersList 属性公开
    • 谢谢,我解决了。为什么它只有在财产公开的情况下才有效?请给我解释一下
    • @meyou 绑定需要公共属性。绑定系统无法访问私有属性或字段。来自MSDNThe properties you use as binding source properties for a binding must be public properties of your class. Explicitly defined interface properties cannot be accessed for binding purposes, nor can protected, private, internal, or virtual properties that have no base implementation
    • 为什么要将整个代码隐藏在DataContext = this 的DataContext 中的每个示例的MainWindow 中?感觉不对,我宁愿将 DataContext 设置为 Collection 并将 ItemsSource 设置为 ItemsSource="{Binding}" 你能解释一下为什么建议它吗?
    • @Silvermind 通常我会推荐 MVVM 设计模式,这意味着 DataContext 是一个 ViewModel 对象,并且与代码隐藏完全无关。看起来发布这个问题的人没有使用 MVVM 设计模式,因为集合是他的 Window 对象的属性,所以最简单的解决方案是将 Window 对象设置为 DataContext
    【解决方案2】:

    您需要在某处设置this.DataContext = this;。这最好在窗口的Load 事件中完成。

    【讨论】:

    • itemsGrid.ItemsSource = this._ordersList 怎么样?
    猜你喜欢
    • 1970-01-01
    • 2021-10-20
    • 2016-09-13
    • 2021-08-31
    • 1970-01-01
    • 2017-11-21
    • 2013-09-07
    • 2013-10-14
    • 1970-01-01
    相关资源
    最近更新 更多