简体   繁体   English

将TextBlock绑定到Observable Collection

[英]Binding TextBlock To Observable Collection

I am trying to bind a textblock to a item in a observable collection. 我试图将文本块绑定到可观察集合中的项目。 Below is the class and the property for the collection. 下面是集合的类和属性。 Am I binding correctly? 我绑定正确吗? Thanks! 谢谢!

public class ListStuff
{
  public string Name { get; set;}
}

private ObservableCollection<ListStuff> mListStuff = new ObservableCollection<ListStuff>();

public ObservableCollection<ListStuff> NameLists
        {
            get
            {
                return mListStuff;
            }
        }

Here is the Xaml if I bind to NameLists it says (collection) 这是Xaml如果我绑定到NameLists它说(集合)

        <Border Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" BorderThickness="0"                        
            <TextBlock Margin="0,5" FontSize="24" HorizontalAlignment="Stretch" Text="{Binding Path=Name"}/>                                                                        
          </Border>

Your "Name" property has to be public. 您的“姓名”属性必须是公开的。 You can bind only against public properties. 您只能绑定公共属性。 Leaving out identifier from property name, means essentially that it becomes a private one, so WPF system can't find it, because it searches only for public properties using reflection. 从属性名称中删除标识符,实际上意味着它变为私有标识符,因此WPF系统无法找到它,因为它仅使用反射搜索公共属性。

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

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