【问题标题】:RadGrid Binding to a ListRadGrid 绑定到列表
【发布时间】:2011-12-15 19:20:10
【问题描述】:

XAML RadGrid

<telerik:RadGridView d:DataContext="{d:DesignInstance {x:Type local:A}}" Name="myGridView" Grid.Column="2" ItemsSource="{Binding Path=MyList}"  Margin="7,7,7,2" IsFilteringAllowed="False" ShowColumnHeaders="True" AutoGenerateColumns="True" />

C#代码

Class A:INotifyPropertyChanged
{
 private List<Fields> MyList;
 public event PropertyChangedEventHandler PropertyChanged;
public List<Fields> _theList
{
  get
  {
    if (MyList == null)
    {
      MyList = new List<Fields>();
    }
    return MyList;
  }
  set
  {
    if (MyList != value)
    {
      MyList = value;
      PropertyChanged(this, new PropertyChangedEventArgs("_theList"));
    }
  }
}    
}

当 MyList 中的项目动态变化时,radgridview 不会自动更新,如果我在代码中重置 Itemssource 就可以了:

mygridview.Itemssource = Null;
mygridview.Itemssource = MyList;

在 MyList 更改后,我每次都必须在代码中重置 itemssource。为什么当 MyList 的内容发生变化时 GridView 不会自动更新? 此外,在设计期间,它向我显示了适当的列标题,列中没有数据,因为列表是空的。但是当我运行应用程序时,列标题消失,并且当 MyList 的内容动态变化时,radgrid 中没有数据显示。

【问题讨论】:

    标签: list binding inotifypropertychanged radgrid


    【解决方案1】:

    When the items in MyList change dynamically,

    当您的列表属性发生更改时,您会收到通知...这不包括上述情况。为了支持你想要的,我认为你需要一个支持 INotifyCollectionChanged 接口的集合。

    http://msdn.microsoft.com/en-us/library/system.collections.specialized.inotifycollectionchanged.aspx

    ObservableCollection 开箱即用地支持这一点。它源自IList&lt;T&gt;。所以也许你可以这样做:

     private IList<Fields> MyList;
     private IList<Fields> MyObservableList;
     public event PropertyChangedEventHandler PropertyChanged;
    public IList<Fields> _theList
    {
      get
      {
        if (MyObservableList == null)
        {
          MyObservableList = new ObservableCollection<Fields>();
        }
        return MyObservableList;
      }
      set
      {
        if (MyList != value)
        {
          MyList = value;
          MyObservableList = new ObservableCollection<Fields>(MyList );
          // this will throw a null reference exception if no one' is listening. You 
          PropertyChanged(this, new PropertyChangedEventArgs("_theList"));
        }
      }
    }  
    

    如果您可以放弃拥有List&lt;T&gt; 实例而拥有ObserableCollection&lt;T&gt; 实例,则上述情况可能会更简单:

       private ObservableCollection<Fields> MyList;
         public event PropertyChangedEventHandler PropertyChanged;
        public ObservableCollection<Fields> _theList
        {
          get
          {
            if (MyList== null)
            {
              MyList= new ObservableCollection<Fields>();
            }
            return MyList;
          }
          set
          {
            if (MyList != value)
            {
              MyList = value;
              // this will throw a null reference exception if no one' is listening. You should make a method OnPropertyChanged that checks if PropertyChanged != null.
              PropertyChanged(this, new PropertyChangedEventArgs("_theList"));
            }
          }
        }
    

    另外,顺便说一句,通常私有成员是 _camelCase 而公共成员是 PascalCase...不确定是否是故意的。

    【讨论】:

    • 为什么我们在这里使用 2 个不同的列表(MyObservableList、My List)?我们不能只使用一个列表并将其绑定到 radgrids itemssource 吗?谢谢。
    • 这就是我所说的让它更简单的意思。我只包括如果您实际上不能将属性设置为 ObservableCollection - 也就是说,您可以用 ObservableCollection (ObservableList) 包装您的列表。单独使用 ObservableCollection 会简单得多。
    • 我会尝试这样做!欣赏!
    猜你喜欢
    • 2011-09-17
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多