我的数据绑定如何写出Length属性? [英] How come my databinding is writing out the Length property?

查看:80
本文介绍了我的数据绑定如何写出Length属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经建立了一个视图模型,它将ObservableCollection<string>绑定到我的DataGrid.

So I've setup a viewmodel to where it binds an ObservableCollection<string> to my DataGrid.

它可以很好地打印出值,但是还可以打印出属性的长度?我不记得曾经在任何绑定中设置该设置.为什么这样做?

It prints out the value just fine but it also prints out the Length of the property? I don't recall ever setting that in any binding whatsoever. Why is it doing that?

我的MainWindow.cs

My MainWindow.cs

public MainWindow()
{
    InitializeComponent();
    DataContext = new MasterViewModel();
}

MasterViewModel.cs

MasterViewModel.cs

class MasterViewModel
{
    public Users Users { get; } = new Users();
    public Achievements Achievements { get; } = new Achievements();
}

Users.cs

class Users : INotifyPropertyChanged
{
    public Users()
    {
        newList.Add("Hello there");
    }

    private ObservableCollection<string> newList = new ObservableCollection<string>();

    public ObservableCollection<string> NewList
    {
        get { return newList; }
        set { newList = value; }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

XAML

<DataGrid ItemsSource="{Binding Users.NewList}" Width="400" Height="200" Margin="182,158,210,61">
    <DataGrid.Columns>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}"></TextBlock>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

推荐答案

DataGrid具有属性AutoGenerateColumns,该属性默认情况下设置为True,并使DataGrid为项中定义的每个属性创建一列.

DataGrid has property AutoGenerateColumns which is set to True by default and makes DataGrid to create a column for each property defined in items.

DataGrid绑定到NewList,其中包含具有Length属性的string类型的项.因此,它使Length

DataGrid is bound to NewList which contains items of type string which has Length property. So it makes Length column

您可以通过设置<DataGrid AutoGenerateColumns="False" ...

这篇关于我的数据绑定如何写出Length属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆