gpt4 book ai didi

wpf - 为 DataGrid 的特定列设置 ItemTemplate

转载 作者:行者123 更新时间:2023-12-05 01:21:38 24 4
gpt4 key购买 nike

我有一个 WPF DataGrid它绑定(bind)到 List<Person> people .

public class Person
{
public string Name{get;set;}
public string LastName{get;set;}
public string Address{get;set;}
public int Age{get;set;}
}

public void ShowPeople()
{
myDataGrid.ItemsSource = people;
}

它显示一切正常,但我想显示 AddressTextBoxDataGrid里面.

我将 XAML 代码更改为:

   <DataGrid x:Name="myDataGrid">
<DataGridTemplateColumn Header="Address">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Address}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid>

这是行不通的。它给我一个错误。

Items collection must be empty before using ItemsSource.

请帮忙。谢谢,

最佳答案

您的 XAML 中缺少 Columns 属性:

<DataGrid x:Name="myDataGrid">
<DataGrid.Columns> <-- This is missing in your code!
<DataGridTemplateColumn Header="Address">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Address}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

关于wpf - 为 DataGrid 的特定列设置 ItemTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15578907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com