sourceupdated事件不触发事件、sourceupdated

2023-09-05 01:45:59 作者:大路朝天,咱俩各走一边。

我有DiscoveredHosts这是一个的ObservableCollection<字符串> 。所述sourceupdated事件不被调用。任何人都知道为什么吗?

 <组合框名称=DiscoveredHostsComboBoxVerticalAlignment =中心Grid.Column =0
    的Horizo​​ntalAlignment =中心=了minWidth100像素的ItemsSource ={绑定路径=
    DiscoveredHosts}SourceUpdated =DiscoveredHostsComboBox_SourceUpdated/>

公共无效GetDomainHosts()
{
    DiscoveredHosts.Clear();

    变种适配器= NetworkInterface.GetAllNetworkInterfaces();

    如果(Config.Debug)
    {
        DiscoveredHosts.Add(192.168.73.11);
        DiscoveredHosts.Add(192.168.73.14);
    }

    的foreach(VAR物业adapters.Select(适配器=> adapter.GetIPProperties()))
    {
        如果(properties.DnsSuffix =&放大器;!&安培;!DiscoveredHosts.Contains(
properties.DnsSuffix))
            DiscoveredHosts.Add(properties.DnsSuffix);

        如果(properties.DnsAddresses.Count< = 0)继续;

        的foreach(在properties.DnsAddresses.Where VAR主机(主机=>!DiscoveredHosts。
任何(一个= gt;一种== host.ToString())))
            DiscoveredHosts.Add(host.ToString());
    }

    OnPropertyChanged(DiscoveredHosts);
}
 

解决方案

请,设置NotifyOnSourceUpdated为true SourceUpdated事件火了:

 <组合框名称=DiscoveredHostsComboBoxVerticalAlignment =中心Grid.Column =0的Horizo​​ntalAlignment =中心=了minWidth100像素的ItemsSource ={绑定路径= DiscoveredHosts ,NotifyOnSourceUpdated = TRUE}SourceUpdated =DiscoveredHostsComboBox_SourceUpdated/>
 
当 代码农 遇上 码农 揭秘主干开发的那些事儿 京东云技术团队

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

解决方案

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