Answer the question
In order to leave comments, you need to log in
Why is data from ObservableCollection not showing up in ListView?
Please help, what am I doing wrong?
I have the following xml file
<phones>
<phone>
<id>1</id>
<title>5685</title>
<company>VZ</company>
<company1>MS</company1>
<company2>RS</company2>
</phone>
<phone>
<id>2</id>
<title>6958</title>
<company>VZ</company>
<company1>MS</company1>
<company2>RS</company2>
</phone>
</phones>
ObservableCollection<People> _Peoples;
private string _FileName = "Data.xml";
private void LoadData()
{
if (File.Exists(_FileName))
{
using (var reader = new StreamReader(_FileName))
{
var xs = new XmlSerializer(typeof(ObservableCollection<People>));
_Peoples = (ObservableCollection<People>)xs.Deserialize(reader);
reader.Close();
}
}
else
{
_Peoples = new ObservableCollection<People>();
}
}
public class People
{
public string id { get; set; }
public string title { get; set; }
public string company { get; set; }
public string company1 { get; set; }
public string company2 { get; set; }
}
<listView Name="listview" ItemSourse = "{Binding ElementName = window, Path= = _Peoples}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header = "#" Width = "125">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text = "{Binding id}"></TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header = "Кому" Width = "100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text = "{Binding title}" ></TextBox>
</DataTemplate >
</GridViewColumn.CellTemplate >
</GridViewColumn>
<GridViewColumn Header = "От кого" Width = "100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text = "{Binding company}" ></TextBox>
</DataTemplate >
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header = "Для кого" Width = "500">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text = "{Binding company1}" ></TextBox>
</DataTemplate >
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header = "Для чего" Width = "350">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text = "{Binding company2}" ></TextBox>
</DataTemplate >
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</listView>
Answer the question
In order to leave comments, you need to log in
1. In WPF, you can only bind to properties; in your code , _Peoples is a field.
2. If you want to report a change in an object, then you need to use the INotifyPropertyChanged interface
ObservableCollection reports its change, so you can clear and repopulate the collection
public ObservableCollection<People> Peoples { get; } =
new ObservableCollection<People>();
private void LoadData()
{
Peoples.Clear();
if (File.Exists(_FileName))
{
using (var reader = new StreamReader(_FileName))
{
var xs = new XmlSerializer(typeof(People[]));
var arr = (People[])xs.Deserialize(reader);
foreach(var p in arr)
{
Peoples.Add(p);
}
reader.Close();
}
}
}
ItemSourse = "{Binding ElementName = window, Path= = _Peoples}"
ItemSourse = "{Binding Peoples}"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question