我如何定义一个WPF DataGrid中我自己的列? [英] How can I define my own columns in a WPF DataGrid?

查看:164
本文介绍了我如何定义一个WPF DataGrid中我自己的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的AutoGenerateColumns WPF的DataGrid中得到必将在code-背后LINQ到SQL,它工作正常。

但是,当我脱下德的AutoGenerateColumns和定义自己的列,它告诉我的项目集合必须在使用ItemsSource前空。

但我没有约束力的ItemSource在我的XAML,所以我不明白为什么它不是空的。 我需要做什么改变,这样我可以定义自己的列?

XAML:

 <用户控件X:类=TestDataGrid566.AppPages.ManageCustomers
    的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
    的xmlns:工具箱=htt​​p://schemas.microsoft.com/wpf/2008/tool​​kit
    加载=UserControl_Loaded>
    <工具包:DataGrid的X:名称=TheDataGrid
                              CanUserAddRows =假
                              AlternatingRowBackground =#DDD
                              CanUserSortColumns =真
                              previewKeyDown =TheDataGrid_ previewKeyDown
                              的AutoGenerateColumns =FALSE
                              RowEditEnding =TheDataGrid_RowEditEnding>
        <工具箱:DataGridTextColumn标题=联系人姓名WIDTH =SizeToCells
                                           绑定={结合联系人姓名}
                                           的IsReadOnly =FALSE/>
    < /工具包:DataGrid的>
< /用户控件>
 

code-背后:

 公共部分类ManageCustomers:用户控件
{
    私人NorthwindDataContext _db =新NorthwindDataContext();

    公共ManageCustomers()
    {
        的InitializeComponent();
    }

    私人无效UserControl_Loaded(对象发件人,RoutedEventArgs E)
    {
        LoadData();
    }

    公共无效LoadData()
    {
        VAR的客户从C在_db.Customers =
                        选择C;
        TheDataGrid.ItemsSource = customers.ToList();
    }
 

解决方案

您正试图直接把列到的DataGrid (因此它试图把列在网格中的产品收集和解释你的错误)。你需要把它放在栏目里面系列:

 <工具包:DataGrid的X:名称=TheDataGrid
                          CanUserAddRows =假
                          AlternatingRowBackground =#DDD
                          CanUserSortColumns =真
                          previewKeyDown =TheDataGrid_ previewKeyDown
                          的AutoGenerateColumns =FALSE
                          RowEditEnding =TheDataGrid_RowEditEnding>
    <工具箱:DataGrid.Columns>
        <工具箱:DataGridTextColumn标题=联系人姓名WIDTH =SizeToCells
                                       绑定={结合联系人姓名}
                                       的IsReadOnly =FALSE/>
    < /工具包:DataGrid.Columns>
< /工具包:DataGrid的>
 

I've got a AutoGenerateColumns WPF-DataGrid getting bound in code-behind to LINQ-to-SQL, which works fine.

But when I take off teh autogeneratecolumns and define my own columns, it tells me "The items collection must be empty before using ItemsSource."

But I'm not binding the ItemSource in my XAML so I don't see why it isn't empty. What do I need to change so that I can define my own columns?

XAML:

<UserControl x:Class="TestDataGrid566.AppPages.ManageCustomers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
    Loaded="UserControl_Loaded">
    <toolkit:DataGrid x:Name="TheDataGrid" 
                              CanUserAddRows="False"
                              AlternatingRowBackground="#ddd"
                              CanUserSortColumns="true"
                              PreviewKeyDown="TheDataGrid_PreviewKeyDown"
                              AutoGenerateColumns="False"
                              RowEditEnding="TheDataGrid_RowEditEnding">
        <toolkit:DataGridTextColumn Header="Contact Name" Width="SizeToCells"  
                                           Binding="{Binding ContactName}" 
                                           IsReadOnly="False"/>
    </toolkit:DataGrid>
</UserControl>

code-behind:

public partial class ManageCustomers : UserControl
{
    private NorthwindDataContext _db = new NorthwindDataContext();

    public ManageCustomers()
    {
        InitializeComponent();
    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        LoadData();
    }

    public void LoadData()
    {
        var customers = from c in _db.Customers
                        select c;
        TheDataGrid.ItemsSource = customers.ToList();
    }

解决方案

You're trying to put the column directly into the DataGrid (therefore it's trying to put the column in the grid's Items collection and that explains your error). You need to put it inside the Columns collection:

<toolkit:DataGrid x:Name="TheDataGrid" 
                          CanUserAddRows="False"
                          AlternatingRowBackground="#ddd"
                          CanUserSortColumns="true"
                          PreviewKeyDown="TheDataGrid_PreviewKeyDown"
                          AutoGenerateColumns="False"
                          RowEditEnding="TheDataGrid_RowEditEnding">
    <toolkit:DataGrid.Columns>        
        <toolkit:DataGridTextColumn Header="Contact Name" Width="SizeToCells"  
                                       Binding="{Binding ContactName}" 
                                       IsReadOnly="False"/>
    </toolkit:DataGrid.Columns>
</toolkit:DataGrid>

这篇关于我如何定义一个WPF DataGrid中我自己的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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