3

I have DiscoveredHosts which is an ObservableCollection<string>. The sourceupdated event is not being called. Anyone know why?

<ComboBox Name="DiscoveredHostsComboBox" VerticalAlignment="Center" Grid.Column="0"
    HorizontalAlignment="Center" MinWidth="100px" ItemsSource="{Binding Path=
    DiscoveredHosts}" SourceUpdated="DiscoveredHostsComboBox_SourceUpdated" />

public void GetDomainHosts()
{
    DiscoveredHosts.Clear();

    var adapters = NetworkInterface.GetAllNetworkInterfaces();

    if (Config.Debug)
    {
        DiscoveredHosts.Add("192.168.73.11");
        DiscoveredHosts.Add("192.168.73.14");
    }

    foreach (var properties in adapters.Select(adapter => adapter.GetIPProperties()))
    {
        if (properties.DnsSuffix != "" && !DiscoveredHosts.Contains(
properties.DnsSuffix))
            DiscoveredHosts.Add(properties.DnsSuffix);

        if (properties.DnsAddresses.Count <= 0) continue;

        foreach (var host in properties.DnsAddresses.Where(host => !DiscoveredHosts.
Any(a => a == host.ToString())))
            DiscoveredHosts.Add(host.ToString());
    }

    OnPropertyChanged("DiscoveredHosts");
}
Sheridan
  • 68,826
  • 24
  • 143
  • 183
Bluebaron
  • 2,289
  • 2
  • 27
  • 37
  • I don't get this: OnPropertyChanged("DiscoveredHosts"); Can you add the declaration of the DiscoveredHosts Property? I think the idea of using ObservableCollection is to avoid calling the OnPropertyChange Method "manually". Something does not look correct IMO. – Marcote Apr 04 '11 at 18:42
  • Oh, yes. I know that's the reason you use an ObservableCollection; however, someone challenged me that that might not be occurring and I had already considered the same issue. I left that line in here so no one would say, "Why don't you try ..." ha ha. public ObservableCollection DiscoveredHosts { get { return _discoveredHosts ?? (_discoveredHosts = new ObservableCollection()); } } – Bluebaron Apr 04 '11 at 19:23

1 Answers1

3

Please, set NotifyOnSourceUpdated to true for SourceUpdated event to fire:

<ComboBox Name="DiscoveredHostsComboBox" VerticalAlignment="Center" Grid.Column="0" HorizontalAlignment="Center" MinWidth="100px"
    ItemsSource="{Binding Path=DiscoveredHosts, NotifyOnSourceUpdated=True}"
    SourceUpdated="DiscoveredHostsComboBox_SourceUpdated" />
Yola
  • 18,496
  • 11
  • 65
  • 106
Dmitry Bogatykh
  • 485
  • 5
  • 10