所以我设置了一个 View 模型到它绑定(bind) ObservableCollection<string>
的位置。到我的数据网格。
它打印出值就好了,但它也打印出属性的长度?我不记得曾经在任何绑定(bind)中设置过它。为什么要这样做?
我的 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/