Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 为DataGrid的特定列设置ItemTemplate_Wpf_Wpf Controls_Wpfdatagrid - Fatal编程技术网

Wpf 为DataGrid的特定列设置ItemTemplate

Wpf 为DataGrid的特定列设置ItemTemplate,wpf,wpf-controls,wpfdatagrid,Wpf,Wpf Controls,Wpfdatagrid,我有一个WPFDataGrid,它被绑定到列出人 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;

我有一个WPF
DataGrid
,它被绑定到
列出人

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;
}
它显示的一切都很好,但是我想在
文本框中显示
地址
,在
数据网格中

我将XAML代码更改为:

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

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

在使用ItemsSource之前,Items集合必须为空

请帮忙。
谢谢,

您缺少XAML中的
属性:

<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>


在代码隐藏中做什么?创建整个
DataGrid
,而不是使用XAML?将ItemTemplate设置为列。我不知道,抱歉。也许你应该发布一个新问题。其他人可能知道:)