List RadGrid绑定到列表

List RadGrid绑定到列表,list,binding,inotifypropertychanged,radgrid,List,Binding,Inotifypropertychanged,Radgrid,XAML雷达栅格 <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

XAML雷达栅格

<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" />
在MyList更改后,每次代码中都必须重置itemssource。为什么当MyList的内容更改时GridView不会自动更新? 此外,在设计期间,它会向我显示适当的列标题,这些列中没有数据,因为列表是空的。但是,当我运行应用程序时,当MyList的内容动态更改时,列标题将消失,并且radgrid中不会显示任何数据。

当MyList中的项目动态更改时

当列表属性发生更改时,您将发出通知…这不包括上述场景。为了支持您想要的,我认为您需要一个支持INotifyCollectionChanged接口的集合

开箱即用的ObservableCollection支持这一点。它源于IList。因此,也许你可以:

 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实例而使用ObserableCollection实例,那么上述操作可能会更简单:

   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…不确定这是否是故意的。

为什么我们在这里使用两个不同的列表MyObservableList,My List?我们不能只使用一个列表并将其绑定到radgrids itemssource吗?谢谢。这就是我说的让它更简单。我只提到,如果你不能将属性设置为ObservableCollection,也就是说,你可以用ObservableCollection ObservableList包装你的列表。单独使用ObservableCollection会简单得多。我将尝试这样做!谢谢你!
 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"));
    }
  }
}  
   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"));
        }
      }
    }