2011-12-15 10 views
0

XAML RadGridRadGridがリストにバインド

<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にデータが表示されません。

答えて

0

When the items in MyList change dynamically,

あなたはときに、リストプロパティの変更...上記のシナリオをカバーしていない通知しています。あなたが望むものをサポートするためには、INotifyCollectionChangedインターフェイスをサポートするコレクションが必要だと思います。箱から出して

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

、のObservableCollectionはこれをサポートしています。それはIList<T>から派生します。だから、おそらくあなたは行うことができます:あなたはObserableCollection<T>インスタンスを持つためList<T>インスタンスを持つforgoeできる場合

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

、さておき、通常はプライベートとしてメンバーは_camelCaseであり、パブリックメンバーはPascalCaseです...意図的なのかどうかわかりません。

+0

ここで2つの異なるリスト(MyObservableList、My List)を使用しているのはなぜですか?私たちはただ一つのリストを使って、それをradgrids itemssourceにバインドできませんか?ありがとう。 – user832219

+0

それは私がそれをより簡単にすることについて言いました。 ObservableCollection(ObservableCollection(ObservableList))でリストをラップすることができます。 ObservableCollectionを単独で使用する方がずっと簡単です。 – Jeff

+0

私はこのようにやろうとします!感謝します! – user832219

関連する問題