How to load Xml-File in ObservableCollection and update the ListView which is bounded to ObservableCollection

  • Thread starter Thread starter BigStiff
  • Start date Start date
B

BigStiff

Guest
Hello, I would like to update my ListView which is bounded to an ObservableCollection. The ObservableCollection is initialized with a Xml-file. But the GridViewColumn of ListView is not updated when I load the Xml-file into the ObservableCollection<Activity>.

CollectionActivity.cs contains an object ObservableCollection of type Activity and is used to save the ObservableCollection into an Xml-File and to load the same Xml-file into an ObservableCollection.

CollectionActivity.cs

namespace ActivityManager
{
public class CollectionActivity : INotifyPropertyChanged
{
private ObservableCollection<Activity> _listView;

public CollectionActivity()
{
_listView = new ObservableCollection<Activity>();
}

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;

if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}

public ObservableCollection<Activity> ListView
{
get { return _listView; }
set
{
_listView = value;
OnPropertyChanged("Intitule");
}
}

public void Save(String filename)
{
XmlSerializer xmlFormat = new XmlSerializer(typeof(ObservableCollection<Activity>));
using (Stream fStream = new FileStream(filename, FileMode.Create,
FileAccess.Write, FileShare.None))
{
xmlFormat.Serialize(fStream, ListView);
}
}

public void Load(String filename)
{
XmlSerializer xmlFormat = new XmlSerializer(typeof(ObservableCollection<Activity>));//typeof(ObservableCollection<>)
using (Stream fstream = File.OpenRead(filename))
{
ListView = xmlFormat.Deserialize(fstream) as ObservableCollection<Activity>;//(ObservableCollection<Activity>)
}
}
}




mainwindow.xaml.cs

namespace ActivityManager
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
...
private CollectionActivity _collectionAct;

public CollectionActivity CollectionAct
{
get { return _collectionAct; }
set { _collectionAct = value; }
}
...
public MainWindow()
{
InitializeComponent();

_collectionAct = new CollectionActivity();


ListViewCollection.ItemsSource = CollectionAct.ListView;
}
//Speichern der ObservableCollection in xml-Datei
private void Save_MenuItem_Click(object sender, RoutedEventArgs e)
{
CollectionAct.Save(@"C:\Users\Daniel\source\repos\Calendrier\activity.xml");
}
//Lesen der xml-Datei in eine ObservableCollection
private void Open_MenuItem_Click(object sender, RoutedEventArgs e)
{
CollectionAct.Load(@"C:\Users\Daniel\source\repos\Calendrier\activity.xml");
}
...
}

mainwindow.xaml

<Grid Grid.Column="0" Grid.Row="0">
<ListView Height="410" Width="210">
<ListView x:Name="ListViewCollection" Height="400" Width="195" SelectionChanged="ListView_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="Intitule " DisplayMemberBinding="{Binding Intitule}"/>
</GridView>
</ListView.View>
</ListView>
</ListView>
</Grid>

Note: ' DisplayMemberBinding="{Binding Intitule}" ' -> Intitule is a member of class Activity

Continue reading...
 
Back
Top