I
I
ilykir2020-09-16 19:48:13
WPF
ilykir, 2020-09-16 19:48:13

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>

I want to load it into an ObservableCollection
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; }
}

And I want to display it in a ListView
<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>

But the data is not displayed, what could be the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2020-09-17
@yarosroman

I recommend you read about the MVVM pattern.

F
Foggy Finder, 2020-09-19
@FoggyFinder

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>();

then the LoadData method will look something like this
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();
        }
    }
}

3. If the DataContext is set correctly, then instead of
ItemSourse = "{Binding ElementName = window, Path= = _Peoples}"

will suffice
ItemSourse = "{Binding Peoples}"
Tip: stick to conventions - no underscores in property names.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question