【问题标题】:Listbox Not binding to a BindingList<T>列表框未绑定到 BindingList<T>
【发布时间】:2011-11-08 16:57:51
【问题描述】:

我的表单上有一个 ListBox,它在后面的代码中绑定到 BindingList&lt;T&gt;,但没有显示 BindingList&lt;T&gt; 中的项目。

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:stackoverflow.com/questions/4284663/…
  • 我将 BindingList 更改为 ObservableCollection 类型,这没有问题,我必须做的唯一更改是从列表中添加/删除项目时,我必须在 UI 线程上执行此操作很容易实现(对于那些不知道的人)使用... Dispatcher.BeginInvoke(new Action(() => ConnectedClients.Add(client)));

标签: c# wpf data-binding


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    尝试使用ObservableCollection&lt;T&gt;。它是专为 WPF 设计的。

    【讨论】:

    • 猜猜我应该阅读有关您问题的 cmets。 @ShelbyZ 您应该将其作为答案发布,而不是放在评论中。
    • 我是对前进方向的猜测,因为我没有时间自己解决。
    【解决方案3】:

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

    您需要将绑定更改为DataContext.ConnectedClients 以绑定到Window.DataContext.ConnectedClients

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

    【讨论】:

    • 或者,只需绑定到 ConnectedClients,因为它显然是 DataContext 中实例的属性
    • @Flq 是的,可以使用给定的代码示例,但如果这是一个简化的代码示例并且 ListBox DataContext 与 Window DataContext 不同,则它不会工作
    • @Rachel 不确定你说它不是存在的属性是什么意思?列表是 Window 类的属性,确实存在??
    • @MrsNezbit 抱歉,您以为您使用的是 MVVM 设计模式。由于您没有使用它并且集合是 Window 类的属性,因此绑定很好。
    猜你喜欢
    • 2021-11-04
    • 2012-10-25
    • 2011-01-30
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    相关资源
    最近更新 更多