データを正しくバインドするのに膨大な量の問題が発生しています。私は似たような問題を抱えている人たちからここの記事を読んだことがありますが、何らかの理由でそれをクリックすることができません。ObservableCollectionをListViewにバインドする
私のテーブルのXMLは次のとおりです。私は自分のコード内
<Window ... DataContext="{Binding RelativeSource={RelativeSource Self}}" >
...
<ListView Height="124" HorizontalAlignment="Left" Margin="12,46,0,0" Name="listViewDocuments" VerticalAlignment="Top" Width="Auto" DataContext="{Binding DocumentList}">
<ListView.View>
<GridView>
<GridViewColumn Width="160" Header="Description" DisplayMemberBinding="{Binding Description}"/>
<GridViewColumn Width="160" Header="Date Filed" DisplayMemberBinding="{Binding DateFiled}"/>
<GridViewColumn Width="160" Header="Filed By" DisplayMemberBinding="{Binding UserFiledName}"/>
<GridViewColumn Width="150" Header="Page" DisplayMemberBinding="{Binding Pages}"/>
<GridViewColumn Width="150" Header="Notes" DisplayMemberBinding="{Binding Notes}"/>
<GridViewColumn Width="Auto" Header="" />
</GridView>
</ListView.View>
</ListView>
:
public ObservableCollection<Document> _DocumentList = new ObservableCollection<Document>();
...
public ObservableCollection<Document> DocumentList{ get { return _DocumentList; } }
...
public class Document
{
public string Description { get; set; }
public string DateFiled { get; set; }
public string UserFiledName { get; set; }
public string Pages { get; set; }
public string Notes { get; set; }
public string Tag { get; set; }
}
私が使用テーブル更新しようとする試みには:
_DocumentList.Add(new Document
{
Description = dr["Description"].ToString(),
DateFiled = dr.GetDateTime(dr.GetOrdinal("DateFiled")).ToShortDateString(),
UserFiledName = dr["UserFiledName"].ToString(),
Pages = dr.GetInt32(dr.GetOrdinal("Pages")).ToString(),
Notes = dr["Notes"].ToString(),
Tag = dr["FileID"].ToString()
});
新しいアイテムを正しく追加されているようですが、listViewでは何も更新されません。
私はこのようなチュートリアルを読んでいる:http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-1
そして、私は他の記事で提案された通知コードのすべてを追加しようとしています。何も私のために働いていません。
アイデアをいただければ幸いです。
実行時にVSの出力ウィンドウをチェックしましたか?バインディングエラーはありますか? 'DataContext'はどこに設定しますか? – nemesv
ああ、タグのDataContext = "{バインディングRelativeSource = {RelativeSource Self}}"です。 –