我正在创建WPF控件库并尝试在DataGrid
中创建动态列,但在下面提到错误。
类型或命名空间名称' DataGridTextColumn'找不到(你错过了使用指令或汇编引用吗?)
请建议我哪里出错了。
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>
答案 0 :(得分:0)
您正在WpfTookit
使用DataGrid
dataGrid1.Columns.Add(textcol);
期待Microsoft.Windows.Controls.DataGridColumn
。
您可以将WpfTookit
更改为内置DataGrid
,也可以更改DataGridTextColumn
:
var textcol = new Microsoft.Windows.Controls.DataGridTextColumn();
答案 1 :(得分:0)
<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>