Andy Clark
Andy Clark

Reputation: 3523

Listbox Not binding to a BindingList<T>

I have a ListBox on a form that is bound to a BindingList<T> in code behind but is not displaying the items within the BindingList<T>.

XAML:

<Window x:Class="MessageServer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MessageServer"
    Name="mainWindow" Title="Message Exchange Server" 
    Height="350" Width="525" Closing="Window_Closing">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <ListBox Name="OutputList" Grid.Row="0" />
        <ListBox Name="Connected" Grid.Row="1" ItemsSource="{Binding ElementName=mainWindow, Path=ConnectedClients}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=FullIPAddress}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>
</Grid>

CodeBehind:

private BindingList<Client> _ConnectedClients;
public BindingList<Client> ConnectedClients
{
    get { return _ConnectedClients; }
    set { _ConnectedClients = value; }
}

public class Client : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private TcpClient _tcpClient;
    public TcpClient tcpClient
    {
        get { return _tcpClient; }
        set
        {
            _tcpClient = value;
            NotifyPropertyChanged();
        }
    }

    public string FullIPAddress
    {
        get { return _tcpClient.Client.RemoteEndPoint.ToString(); }
    }

    public string IPAddress
    {
        get { return _tcpClient.Client.RemoteEndPoint.ToString().Split(':').ElementAt(0); }
    }

    public string PortNumber
    {
        get { return _tcpClient.Client.RemoteEndPoint.ToString().Split(':').ElementAt(1); }
    }

    public Client(TcpClient tcpClient)
    {
        this.tcpClient = tcpClient;
        NotifyPropertyChanged();
    }

    private void NotifyPropertyChanged()
    {
        NotifyPropertyChanged("tcpClient");
        NotifyPropertyChanged("FullIPAddress");
        NotifyPropertyChanged("IPAddress");
        NotifyPropertyChanged("PortNumber");
    }

    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Any Ideas why the list box is not displaying the items? Not sure if this is worth mentioning but When added items to the BindingList this is done on a seperate thread to the UI Thread. but I have tried using Dispatcher.BeginInvoke() but still does not work...

Upvotes: 1

Views: 653

Answers (3)

ShelbyZ
ShelbyZ

Reputation: 1504

It sounds like you really want to use ObservableCollection. It sounds like BindingList should work, but on this SO post they seem say ObservableCollection is for WPF and BindingList for Winforms: Differences between BindingList and ObservableCollection

Upvotes: 3

Nick O
Nick O

Reputation: 3826

Try using an ObservableCollection<T>. It was designed specifically for WPF.

Upvotes: 2

Rachel
Rachel

Reputation: 132548

You are trying to bind to Window.ConnectedClients, which is a property that doesn't exist.

You need to change your binding to DataContext.ConnectedClients to bind to Window.DataContext.ConnectedClients

ItemsSource="{Binding ElementName=mainWindow, Path=DataContext.ConnectedClients}"

Upvotes: 1

Related Questions