asaf shabat

asaf shabat

  • NA
  • 1
  • 1.2k

Unable to update the user interface using ObservableCollecti

Jan 8 2014 4:37 AM
Hello (:

I'm trying to update the UI using ObservableCollection but can not .. I think my mistake derives from the problem of DataBinding.

The Entity class - only get & set string ststus of the ping replay.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pinger
{
    public class Entity
    {
        public Entity(string status)
        {
            _status = status;
        }
        private string _status;

        public string Status
        {
            get { return _status; }
            set { _status = value; }
        }
        
    }
}
PingManager class that derives from ObservableCollection<Entity>

and i make Add(new Entity(status.ToString()));

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Threading;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Pinger
{
    class PingManager : ObservableCollection<Entity>
    {
        private List<string> _ipAddressList;
        private Ping pingSender;
        private List<Ping> _listOfPingObjects;

        public PingManager(System.Collections.IEnumerable listOfIpAddress)
        {
            _listOfPingObjects = new List<Ping>();
            _ipAddressList = new List<string>();
            foreach (var ipAddress in listOfIpAddress)
            {
                _ipAddressList.Add(ipAddress.ToString());
            }
        }

        internal void StartPing()
        {
            foreach (var pingItem in _ipAddressList)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPing), pingItem);
            }
        }

        private void ThreadPing(object ipAddress)
        {
            AutoResetEvent waiter = new AutoResetEvent (false);
            pingSender = new Ping();
            pingSender.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
            pingSender.SendAsync(ipAddress.ToString(), waiter);
            _listOfPingObjects.Add(pingSender);
            waiter.WaitOne();
        }

        private void PingCompletedCallback(object sender, PingCompletedEventArgs e)
        {
            PingReply replay = e.Reply;
            var address = e.Reply.Address;
            var status = e.Reply.Status;
            var roundTripTime = e.Reply.RoundtripTime;
            var ttl = e.Reply.Options.Ttl;
            string allReplayInfo = status.ToString() + " " + address.ToString() + " " + roundTripTime.ToString() + " " + ttl.ToString();
            Add(new Entity(status.ToString()));
        }
    }
}
The code behind of MainWindow

I do not know whether to write here something related to-ObservableCollection, DataBinding, DataContext????

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Pinger
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private PingManager _pinger;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void btn_addIpAddress_Click(object sender, RoutedEventArgs e)
        {
            if (tb_ipAddressOct1.Text == "" || tb_ipAddressOct2.Text == "" || tb_ipAddressOct3.Text == "" || tb_ipAddressOct4.Text == "")
            {
                return;
            }
            string ipAddress = tb_ipAddressOct1.Text + "." + tb_ipAddressOct2.Text + "." + tb_ipAddressOct3.Text + "." + tb_ipAddressOct4.Text;
            if (!cb_ipAddressList.Items.Contains(ipAddress))
            {
                cb_ipAddressList.Items.Add(ipAddress);
                cb_ipAddressList.SelectedIndex = 0;
            }
           
        }

        private void btn_pingIt_Click(object sender, RoutedEventArgs e)
        {
            _pinger = new PingManager(cb_ipAddressList.Items);
            _pinger.StartPing();
        }
    }
}
I don't know if the Window.Resources is ok or not...?

i just need to update ListBox when i get replay status of ping.

<Window x:Class="Pinger.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Pinger"
        Title="Pinger" Height="350" Width="525">
    <Window.Resources>
        <ObjectDataProvider x:Key="PingManagerDataSorce" ObjectType="{x:Type local:PingManager}"/>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="274*"></ColumnDefinition>
            <ColumnDefinition Width="229*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox Name="tb_ipAddressOct1" Width="40" Height="25" Margin="70,42,142,37"></TextBox>
        <TextBox Name="tb_ipAddressOct2" Width="40" Margin="127,42,107,37"></TextBox>
        <TextBox Name="tb_ipAddressOct3" Width="40" Height="25" Margin="162,42,50,37"></TextBox>
        <TextBox Name="tb_ipAddressOct4" Width="40" Height="25" Margin="208,42,4,37"></TextBox>
        <Label Content="IP Address:" Height="28" HorizontalAlignment="Left" Margin="0,40,0,0" Name="label1" VerticalAlignment="Top" Width="75" />
        <ComboBox Grid.Column="1" Height="26" HorizontalAlignment="Left" Margin="65,42,0,0" Name="cb_ipAddressList" VerticalAlignment="Top" Width="152" />
        <Button Content="Add" Grid.Column="1" Height="26" HorizontalAlignment="Left" Margin="13,42,0,0" Name="btn_addIpAddress" VerticalAlignment="Top" Width="46" Click="btn_addIpAddress_Click" />
        <Menu Height="23" HorizontalAlignment="Left" Name="menu1" VerticalAlignment="Top" Width="513" Grid.ColumnSpan="2">
            <MenuItem Header="File">
                <MenuItem Header="Exit" />
            </MenuItem>
        </Menu>
        <Button Content="Start ping" Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="142,0,0,0" Name="btn_pingIt" VerticalAlignment="Top" Width="75" Click="btn_pingIt_Click" />
        
        <ListBox ItemsSource="{Binding Source={StaticResource PingManagerDataSorce}}" Grid.Row="2" Height="99" HorizontalAlignment="Left" Name="lb_pingReplay" VerticalAlignment="Top" Width="503" Grid.ColumnSpan="2">
           
        </ListBox>
    </Grid>
</Window>
Anyway, the idea is that when there is an update to the Status property in the ListBox be able to get this update and display it.

Help me if you could demonstrate the solution on my code (:

Thank you so much for helping!!!

Answers (1)