I put in some values to ListView. I do it in MainWindow() method, and later. Although later added data can be changed, the values I put in the MainWindow() method cannot be changed the main window class I work loads data from xml file and enters to ObservationCollection
ObservableCollection<MonitorData> monitorList = new ObservableCollection<MonitorData>();
public ObservableCollection<MonitorData> MonitorList
{
get
{
return monitorList;
}
}
public MainWindow()
{
DataContext = this;
InitializeComponent();
using (XmlReader reader = XmlReader.Create(@"/monitor.xml"))
{
string name = string.Empty;
string address = string.Empty;
string type = string.Empty;
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name.ToString())
{
case "name":
name = reader.ReadElementContentAsString();
break;
case "address":
address = reader.ReadElementContentAsString();
break;
case "type":
type = reader.ReadElementContentAsString();
monitorList.Add(new MonitorData() { Name = name, Address = address, Type = type });
break;
}
}
}
}
the xaml - there I try to present data from xml file
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Monitor" x:Class="Monitor.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="1332.047" Width="810">
<Window.Resources>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</Window.Resources>
<Grid>
<ListView Margin="5" x:Name="MonitoringListView" ItemsSource="{Binding MonitorList}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="200" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Address" Width="200" DisplayMemberBinding="{Binding Address}"/>
<GridViewColumn Header="Type" Width="200" DisplayMemberBinding="{Binding Type}"/>
<GridViewColumn Header="Availability" Width="200" DisplayMemberBinding="{Binding Enabled}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
class I work with
public class MonitorData
{
public string Name { get; set; }
public string Address { get; set; }
public string Type { get; set; }
public bool Enabled { get; set; }
}
I add data every 10 seconds and try to change it, it is possible to change data I added in Timer_Tick, but somehow fail to change the data I entered in the MainWindow() from xml file
private void Timer_Tick(object sender, EventArgs e)
{
monitorList.Add(new MonitorData() { Name = "new", Address = "111", Type = "sometype", Enabled = true });
for (int i = 0; i < monitorList.Count; i++)
{
monitorList[i].Name = "new name";
monitorList[i].Enabled = true;
}
}