질문에 제한된 정보에서 나는 당신의 데이터 테이블은 내가 당신의 코드가 훨씬 더 복잡하기 때문에 권하고 싶지 않다 사용자 정의 형식의 열을 포함 추측하고있다. 그러나 그런 식으로 결정한다면 사용자 정의 열 유형을 구현하여이를 처리 할 수 있습니다. 나는 그것을 시도하지는 않았지만,이 링크는 유망 해 보입니다. Bind DataTable to WPF DataGrid using DataGridTemplateColumn Programatically
동일한 작업을 수행하기 위해 값 변환기를 사용할 수도 있습니다. 아래 예제는 열 인덱스를 매개 변수로 전달하지만 관심있는 속성도 일부 형식 (예 : 0 값, 0 열, 속성 값)으로 전달할 수 있습니다.
XAML : 뒤에
<Window x:Class="GridCellDemo.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
xmlns:GridCellDemo="clr-namespace:GridCellDemo"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<GridCellDemo:CellConverter x:Key="CellConverter" />
</Window.Resources>
<Grid>
<Controls:DataGrid ItemsSource="{Binding Data}" AutoGenerateColumns="False">
<Controls:DataGrid.Columns>
<Controls:DataGridTextColumn Header="Col 0" Binding="{Binding ., Converter={StaticResource CellConverter}, ConverterParameter=0}" />
<Controls:DataGridTextColumn Header="Col 1" Binding="{Binding ., Converter={StaticResource CellConverter}, ConverterParameter=1}" />
</Controls:DataGrid.Columns>
</Controls:DataGrid>
</Grid>
</Window>
코드 :
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
namespace GridCellDemo
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = this;
}
public DataTable Data
{
get
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Col0", typeof(GridCell)));
dt.Columns.Add(new DataColumn("Col1", typeof(GridCell)));
DataRow row0 = dt.NewRow();
dt.Rows.Add(row0);
row0["Col0"] = new GridCell() { Value = "R0C0" };
row0["Col1"] = new GridCell() { Value = "R0C1" };
DataRow row1 = dt.NewRow();
dt.Rows.Add(row1);
row1["Col0"] = new GridCell() { Value = "R1C0" };
row1["Col1"] = new GridCell() { Value = "R1C1" };
return dt;
}
}
}
public class GridCell
{
public string Value { get; set; }
public Guid Id { get; set; }
}
public class CellConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
DataRowView drv = value as DataRowView;
if (drv == null)
{
return string.Empty;
}
int columnIndex = int.Parse(parameter.ToString());
GridCell gridCell = drv[columnIndex] as GridCell;
return gridCell.Value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
이 바인딩 코드를 표시합니다. –
GridCell 컬렉션에 그리드의 itemsource 속성을 할당하고 있습니까? 바인딩 코드를 게시 할 수 있습니까? – Novice
추가 된 바인딩 스 니펫. –