2013-08-27 41 views
0

我有一個網格與服務器列表,當窗口打開時,我請求服務器列表並更新viewmodel。但是這些更改沒有顯示在我的視圖模型中。更新viewmodel時網格不更新

public ServerInfo_ViewModel serverInfoViewModel { get; set; } 

public FindServer2() 
{ 
    InitializeComponent(); 
    this.Loaded += new RoutedEventHandler(WindowLoaded); 
    serverInfoViewModel = new ServerInfo_ViewModel(); 
} 

void WindowLoaded(object sender, RoutedEventArgs e) 
{ 
    Multicast.OnAlarmServerFound += new Multicast.AlarmServerFoundHandler(Multicast_OnAlarmServerFound); 
    Multicast.FindAlarmServers(); 
} 

public delegate void Multicast_OnAlarmServerFoundHandler(string IPAddress, string returnvalue); 
    void Multicast_OnAlarmServerFound(string IPAddress, string returnvalue) 
    { 

     ServerInfo si = new ServerInfo(); 
     si.Server = IPAddress; 

     if (source.Length > 1) 
       si.Version = source[1]; 
     if (source.Length > 2) 
       si.Connection = source[2]; 
     if (source.Length > 3) 
       si.Port = source[3]; 
     if (source.Length > 4) 
       si.HostName = source[4]; 
     if (source.Length > 1) 
     { 
      try 
      { 
       serverInfoViewModel.Servers.Add(si); // This is called 
      } 
      catch 
      {} 
     } 
    } 

這是我的視圖模型

public class ServerInfo_ViewModel : INotifyPropertyChanged 
    { 
     public ServerInfo_ViewModel() 
     { 
      this.Servers = new ObservableCollection<ServerInfo>(); 
      LoadInitialServerList(); 
     } 

     public ObservableCollection<ServerInfo> Servers 
     { 
      get 
      { 
       return servers; 
      } 
      set 
      { 
       servers = value; 
       servers.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(servers_CollectionChanged); 
      } 
     } 

     void servers_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
     { 
      // Here you will be informed, if the content of the collection has been changed. 
      OnPropertyChanged("Servers"); 
     } 

     private ObservableCollection<ServerInfo> servers; 

     private void LoadInitialServerList() 
     { 
      servers.Add(new ServerInfo("Test", "Test", "Test", "Test", "Test")); 
     } 

     #region INotifyPropertyChanged Members 

     public event PropertyChangedEventHandler PropertyChanged; 

     public void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

     #endregion 
    } 
} 

和XAML

<Window x:Class="Digicom.DESDigitelClientWPF.FindServer2" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow" Height="350" Width="525" 
xmlns:local="clr-namespace:Digicom.DESDigitelClientWPF" 
> 

<Window.DataContext> 
    <local:ServerInfo_ViewModel/> 
</Window.DataContext> 

<StackPanel> 
    <DataGrid ItemsSource="{Binding Servers, Mode=TwoWay}" Height="132" Width="442" AutoGenerateColumns="False" GridLinesVisibility="None"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Binding="{Binding HostName}" Header="Server"/> 
     <DataGridTextColumn Binding="{Binding Server}" Header="IP" /> 
     <DataGridTextColumn Binding="{Binding Version}" Header="Version" /> 
     <DataGridTextColumn Binding="{Binding Connection}" Header="Connection" /> 
     <DataGridTextColumn Binding="{Binding Port}" Header="Port" /> 
    </DataGrid.Columns> 
</DataGrid> 
</StackPanel> 

當我運行它,我得到這個,顯示最初的對象,而不是第二是在運行時被添加。

enter image description here

回答

2

從你的XAML文件中刪除DataContext的定義,並在代碼隱藏添加。 您的代碼創建ViewModel的兩個實例。您已將ServerInfo添加到未綁定到View的實例,因此無法看到更改。

視圖模型

public class ServerInfo_ViewModel : INotifyPropertyChanged 
{ 
    public ServerInfo_ViewModel() 
    { 
     this.Servers = new ObservableCollection<ServerInfo>(); 
     LoadInitialServerList(); 
    } 

    public ObservableCollection<ServerInfo> Servers 
    { 
     get 
     { 
      return servers; 
     } 
     set 
     { 
      if(servers != value) 
      { 
       servers = value; 
       OnPropertyChanged("Servers"); 
      } 
     } 
    } 

    private ObservableCollection<ServerInfo> servers; 

    private void LoadInitialServerList() 
    { 
     servers.Add(new ServerInfo("Test", "Test", "Test", "Test", "Test")); 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    public void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    #endregion 
} 

}

XAML:

<Window x:Class="Digicom.DESDigitelClientWPF.FindServer2" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow" Height="350" Width="525" 
xmlns:local="clr-namespace:Digicom.DESDigitelClientWPF" 
> 
<StackPanel> 
    <DataGrid ItemsSource="{Binding Servers, Mode=TwoWay}" Height="132" Width="442" AutoGenerateColumns="False" GridLinesVisibility="None"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Binding="{Binding HostName}" Header="Server"/> 
     <DataGridTextColumn Binding="{Binding Server}" Header="IP" /> 
     <DataGridTextColumn Binding="{Binding Version}" Header="Version" /> 
     <DataGridTextColumn Binding="{Binding Connection}" Header="Connection" /> 
     <DataGridTextColumn Binding="{Binding Port}" Header="Port" /> 
    </DataGrid.Columns> 
</DataGrid> 
</StackPanel> 

代碼隱藏:

public ServerInfo_ViewModel serverInfoViewModel { get; set; } 

public FindServer2() 
{ 
    InitializeComponent(); 
    this.Loaded += new RoutedEventHandler(WindowLoaded); 
    serverInfoViewModel = new ServerInfo_ViewModel(); 
    this.DataContext = serverInfoViewModel; 

} 

void WindowLoaded(object sender, RoutedEventArgs e) 
{ 
    Multicast.OnAlarmServerFound += new Multicast.AlarmServerFoundHandler(Multicast_OnAlarmServerFound); 
    Multicast.FindAlarmServers(); 
} 

public delegate void Multicast_OnAlarmServerFoundHandler(string IPAddress, string returnvalue); 
    void Multicast_OnAlarmServerFound(string IPAddress, string returnvalue) 
    { 

     ServerInfo si = new ServerInfo(); 
     si.Server = IPAddress; 

     if (source.Length > 1) 
       si.Version = source[1]; 
     if (source.Length > 2) 
       si.Connection = source[2]; 
     if (source.Length > 3) 
       si.Port = source[3]; 
     if (source.Length > 4) 
       si.HostName = source[4]; 
     if (source.Length > 1) 
     { 
      try 
      { 
       serverInfoViewModel.Servers.Add(si); // This is called 
      } 
      catch 
      {} 
     } 
    } 
+0

是的,就是這樣。謝謝! –