2017-11-21 7 views
0

私はいくつかの値をListViewに入れました。私はMainWindow()メソッドとそれ以降でそれを行います。後でデータを変更することができます追加ものの、私はメインウィンドウ()メソッドに入れた値が メインウィンドウクラスを変更することができない私は、XMLファイルからデータをロードを仕事とデフォルト値を変更することはできませんListViewに追加

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; 
        } 
       } 
      } 
     } 

にXAMLをObservationCollectionするために入った - そこに私がしてみてくださいクラス

<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> 

XMLファイルからデータを提示するために、私は

public class MonitorData 
{ 
    public string Name { get; set; } 
    public string Address { get; set; } 
    public string Type { get; set; } 
    public bool Enabled { get; set; } 
} 
と連携します

私は私がTimer_Tickに追加されたデータを変更することは可能ですが、どういうわけか、私はメインウィンドウ(に入力されたデータを変更することができないデータごとに10秒を追加し、それを変更しようとすると、 )xmlファイルから

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; 
     } 

    } 
+0

Impliment [INotifyPropertyChanged](https://stackoverflow.com/a/1316417/2470362)インターフェイス –

答えて

0

更新を表示する場合は、MonitorDataクラスでINotifyPropertyChangedインターフェイスを実装する必要があります。

関連する問題