Silverlight - 无法将 ListBox 绑定到我的 ObservableCollection

发布于 2024-09-10 19:05:37 字数 1397 浏览 7 评论 0原文

我正在尝试创建一个 ListBox,只要该集合中的任何内容发生更改,它就会更新到 ObservableCollection 的内容,因此这是我为此编写的代码:

xaml:

<ListBox x:Name="UserListTest" Height="300" Width="200" ItemsSource="listOfUsers">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding LastName}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox> 

C#:

public ObservableCollection<User> listOfUsers
    {
        get { return (ObservableCollection<User>)GetValue(listOfUsersProperty); }
        set { SetValue(listOfUsersProperty, value); }
    }

    public static readonly DependencyProperty listOfUsersProperty =
        DependencyProperty.Register("listOfUsers", typeof(ObservableCollection<User>), typeof(MainPage), null);

我设置了对 WCF 服务的调用填充 listOfUsers:

void repoService_FindAllUsersCompleted(object sender, FindAllUsersCompletedEventArgs e)
    {
        this.listOfUsers = new ObservableCollection<User>();

        foreach (User u in e.Result)
        {
            listOfUsers.Add(u);
        }

        //Making sure it got populated
        foreach (User u in listOfUsers)
        {
            MessageBox.Show(u.LastName);
        }
    }

ListBox 永远不会填充任何内容。我认为我的问题可能出在 xaml 上,因为 ObservableCollection 实际上包含我的所有用户。

I'm trying to make a ListBox that updates to the contents of an ObservableCollection whenever anything in that collection changes, so this is the code I've written for that:

xaml:

<ListBox x:Name="UserListTest" Height="300" Width="200" ItemsSource="listOfUsers">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding LastName}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox> 

C#:

public ObservableCollection<User> listOfUsers
    {
        get { return (ObservableCollection<User>)GetValue(listOfUsersProperty); }
        set { SetValue(listOfUsersProperty, value); }
    }

    public static readonly DependencyProperty listOfUsersProperty =
        DependencyProperty.Register("listOfUsers", typeof(ObservableCollection<User>), typeof(MainPage), null);

And I set up a call to a WCF Service that populates listOfUsers:

void repoService_FindAllUsersCompleted(object sender, FindAllUsersCompletedEventArgs e)
    {
        this.listOfUsers = new ObservableCollection<User>();

        foreach (User u in e.Result)
        {
            listOfUsers.Add(u);
        }

        //Making sure it got populated
        foreach (User u in listOfUsers)
        {
            MessageBox.Show(u.LastName);
        }
    }

The ListBox never populates with anything. I assume my problem may be with the xaml since the ObservableCollection actually has all of my Users in it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫性小仙女 2024-09-17 19:05:38

您缺少 ItemsSource 中的 {Binding} 部分。

<ListBox x:Name="UserListTest" Height="300" Width="200" ItemsSource="{Binding listOfUsers}"> 
    <ListBox.ItemTemplate> 
        <DataTemplate> 
            <TextBlock Text="{Binding LastName}" /> 
        </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox>  

另外,您的列表可能不需要 DependencyProperty,您可以使用实现 INotifyPropertyChanged 的​​类上的属性来实现您所需要的。这可能是一个更好的选择,除非您出于某种其他原因需要 DependencyProperty(以及随之而来的开销)。

例如

public class MyViewModel : INotifyPropertyChanged
{
    private ObservableCollection<User> _listOfUsers;
    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<User> ListOfUsers
    {
        get { return _listOfUsers; }
        set
        {
            if (_listOfUsers == value) return;
            _listOfUsers = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("ListOfUsers"));
        }
    }

}

You're missing the {Binding} part from your ItemsSource there.

<ListBox x:Name="UserListTest" Height="300" Width="200" ItemsSource="{Binding listOfUsers}"> 
    <ListBox.ItemTemplate> 
        <DataTemplate> 
            <TextBlock Text="{Binding LastName}" /> 
        </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox>  

Also, you may not need to have a DependencyProperty for your list, you may be able to achieve what you need with a property on a class that implements INotifyPropertyChanged. This may be a better option unless you need a DependencyProperty (and the overhead that goes along with it) for some other reason.

e.g.

public class MyViewModel : INotifyPropertyChanged
{
    private ObservableCollection<User> _listOfUsers;
    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<User> ListOfUsers
    {
        get { return _listOfUsers; }
        set
        {
            if (_listOfUsers == value) return;
            _listOfUsers = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("ListOfUsers"));
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文