Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新viewmodel时网格未更新_C#_Wpf_Mvvm - Fatal编程技术网

C# 更新viewmodel时网格未更新

C# 更新viewmodel时网格未更新,c#,wpf,mvvm,C#,Wpf,Mvvm,我有一个包含服务器列表的网格,当窗口打开时,我请求一个服务器列表,然后更新viewmodel。但这些更改不会显示在我的viewmodel中 public ServerInfo_ViewModel serverInfoViewModel { get; set; } public FindServer2() { InitializeComponent(); this.Loaded += new RoutedEventHandler(WindowLoaded); server

我有一个包含服务器列表的网格,当窗口打开时,我请求一个服务器列表,然后更新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
            {}
        }
    }
这是我的viewmodel

    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
    }
}
公共类ServerInfo\u视图模型:INotifyPropertyChanged
{
公共服务器信息_视图模型()
{
this.Servers=newobserveCollection();
LoadInitialServerList();
}
公共可观测收集服务器
{
得到
{
返回服务器;
}
设置
{
服务器=价值;
servers.CollectionChanged+=新系统.Collections.Specialized.NotifyCollectionChangedEventHandler(servers\u CollectionChanged);
}
}
无效服务器\u CollectionChanged(对象发送方,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
//如果集合的内容已更改,将在此处通知您。
OnPropertyChanged(“服务器”);
}
专用可观测收集服务器;
私有void LoadInitialServerList()
{
添加(新的ServerInfo(“测试”、“测试”、“测试”、“测试”、“测试”));
}
#区域INotifyProperty更改成员
公共事件属性更改事件处理程序属性更改;
公共void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
#端区
}
}
和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>

当我运行它时,我得到了这个结果,初始对象被显示,但不是在运行时添加的第二个对象


从XAML文件中删除Datacontext定义,并将其添加到Codebehind中。 您的代码创建了ViewModel的两个实例。您已将ServerInfo添加到未绑定到视图的实例中,因此无法看到更改

视图模型

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
}

是的,就是这样。谢谢
<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
            {}
        }
    }