在 WPF 控制库中获取错误

Getting Error in WPF contol Library

我正在创建 WPF 控件库并尝试在 DataGrid 中创建动态列,但收到下面提到的错误。

The type or namespace name 'DataGridTextColumn' could not be found (are you missing a using directive or an assembly reference?)

请指出我哪里出错了。

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Data;
using System.Collections.ObjectModel;

namespace TestWPFLibrary
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
            //Create a new column to add to the DataGrid
            var textcol = new DataGridTextColumn();
            //Create a Binding object to define the path to the DataGrid.ItemsSource property
            //The column inherits its DataContext from the DataGrid, so you don't set the source
            Binding b = new Binding("LastName");
            //Set the properties on the new column
            textcol.Binding = b;
            textcol.Header = "Last Name";
            //Add the column to the DataGrid
           dataGrid1.Columns.Add(textcol);

        }
    }
}

XAML

<UserControl x:Class="TestWPFLibrary.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
            xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
             xmlns:my1="clr-namespace:TestWPFLibrary">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <my:DataGrid Height="288" HorizontalAlignment="Left" Margin="0,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="300" ItemsSource="{Binding Path=b}" AutoGenerateColumns="False"/>
    </Grid>
</UserControl>

您在 dataGrid1.Columns.Add(textcol); 中使用 WpfTookit DataGrid 期待 Microsoft.Windows.Controls.DataGridColumn。 您可以将 WpfTookit 更改为内置的 DataGrid,或者更改 DataGridTextColumn:

var textcol = new Microsoft.Windows.Controls.DataGridTextColumn();
<my:DataGrid x:Name="ConfigGrid" AutoGenerateColumns="False">
  <my:DataGrid.Columns>
  <my:DataGridTextColumn Binding="{Binding Path=Entry_Number}" Header="Id" Visibility="Hidden" />
  <my:DataGridTextColumn Binding="{Binding Path=Part_CatalogNumber}" Header="Part Number" />
  <my:DataGridTextColumn Binding="{Binding Path=Sensor_Type}" Header="Sensor Type Id" />
  <my:DataGridTextColumn Binding="{Binding Path=Date_Entered}" Header="Date Created" />
  <my:DataGridTextColumn Binding="{Binding Path=User_Comments}" Header="User Comments" Width="250" />
  <my:DataGridTemplateColumn>
      <my:DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
            <Button Name="btnOldDelete" Click="btnOldDelete_Click">Delete</Button>
         </DataTemplate>
      </my:DataGridTemplateColumn.CellTemplate>
  </my:DataGridTemplateColumn>
  </my:DataGrid.Columns>
  </my:DataGrid>