c# - 列表框未绑定(bind)到 BindingList<T>

标签 c# wpf data-binding

我在绑定(bind)到 BindingList<T> 的表单上有一个 ListBox在代码后面但不显示 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>

代码隐藏:

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

列表框不显示项目的任何想法? 不确定这是否值得一提,但是当将项目添加到 BindingList 时,这是在 UI 线程的单独线程上完成的。但我已经尝试使用 Dispatcher.BeginInvoke() 但仍然不起作用...

最佳答案

听起来您真的很想使用 ObservableCollection。听起来 BindingList 应该可以工作,但是在这篇 SO 帖子上,他们似乎说 ObservableCollection 用于 WPF,而 BindingList 用于 Winforms:Differences between BindingList and ObservableCollection

关于c# - 列表框未绑定(bind)到 BindingList<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8054176/

相关文章:

c# - 从 coldfusion 调用 .net 对象的方法

c# - SharePoint 列表项权限

c# - 将单个数字转换为多于 2 位数字

c# - 使按钮宽度相同

c# - 在 MVVM 中动态创建控件

c# - 找不到 WCF 服务引用命名空间

wpf - 为什么 ComboBox 是下拉菜单导致整个应用程序性能非常慢?

Java相当于WPF

angularjs - Angular 数据绑定(bind)不显示数据..

wpf - 将 WPF 数据网格绑定(bind)到多个数据源