繁体   English   中英

列表框未绑定到BindingList <T>

[英]Listbox Not binding to a BindingList<T>

我有绑定到窗体上的列表框BindingList<T>在后面的代码,但内未显示的项目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: BindingList和ObservableCollection之间的区别

尝试使用ObservableCollection<T> 它是专门为WPF设计的。

您正在尝试绑定到Window.ConnectedClients ,这是一个不存在的属性。

您需要更改对DataContext.ConnectedClients的绑定才能绑定到Window.DataContext.ConnectedClients

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM