c# - 我的数据绑定(bind)怎么会写出 Length 属性?

标签 c# wpf mvvm data-binding datagrid

所以我设置了一个 View 模型到它绑定(bind) ObservableCollection<string> 的位置。到我的数据网格。

它打印出值就好了,但它也打印出属性的长度?我不记得曾经在任何绑定(bind)中设置过它。为什么要这样做?

Visual representation of what it looks like

我的 MainWindow.cs

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

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

用户.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 绑定(bind)到 NewList其中包含 string 类型的项目它具有 Length 属性。所以它使Length柱子

您可以通过设置 <DataGrid AutoGenerateColumns="False" ... 来禁用自动生成

关于c# - 我的数据绑定(bind)怎么会写出 Length 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50390576/

相关文章:

model-view-controller - 业务逻辑是存在于模型中还是存在于 Controller 中?

c# - 数据网格中的复选框选中事件不触发wpf mvvm

c# - 在我的下载器中添加暂停和继续功能

c# - 在 T-SQL 中将 '01-Sep-2017' 转换为 '01/09/2017'?

c# - 数据绑定(bind) WPF 列表框?

WPF 同一系列的多个字体文件

c# - 使用 DocumentViewer 控件呈现 PDF?

c# - 如何摆脱 DataContext InitializeComponent 中的 StackOverflow 异常?

c# - 在 Entity Framework 中将对象添加到数据库时出现问题

c# - 遍历具有相似名称的控件