サーバーのリストを含むグリッドがあり、ウィンドウが開いているときにサーバーのリストを要求し、ビューモデルを更新します。しかし、変更はビューモデルに表示されません。
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>
実行すると、最初のオブジェクトが表示されますが、実行時に追加された 2 番目のオブジェクトは表示されません。