2017-10-15 2 views
1

ComboBox 또는 TextBlock과 같은 열 스타일을 가진 DataGrid에 데이터 집합을 표시해야합니다. DataGrid는 DataTable에 바인딩됩니다. DataTable의 각 열의 수와 위치는 런타임에 정의되므로 프로그래밍 방식으로 DataGrid를 만듭니다. DataGridTextColumn을 사용하고 기본 스타일 (TextBlock)을 유지하는 한 모든 것이 좋지만 DataGridTextColumn을 TextBox 스타일로 변경하려고하면 유형 오류가 발생합니다. 은 콤보에 관한 것에 대해 아무 문제가 없다, 그래서 나는 (하나의 데이터 그리드의 셀) 내 코드의 이하에만 DataGridTextColumn 부분을 붙여 넣습니다DataTable에 대한 WPF DataGrid 바인딩 및 코드 뒤에있는 TextBox 스타일

C#

// Create the DataTable that will contain real-time data 
public DataTable CurrentTable { get; set; } 

// Binding string 
string stringA = "some_string_A"; 

// Create new binding 
Binding b = new Binding(stringA); 
b.Mode = BindingMode.TwoWay; 

// Create a new TextColumn 
DataGridTextColumn dgCol = new DataGridTextColumn(); 

//dgCol.ElementStyle = new Style(typeof(TextBox)); <- this row generates error 

// Set the column binding for the new TextColumn 
dgCol.Binding = b; 

// Add the TextColumn to the DataGrid 
datagrid.Columns.Add(dgCol); 

// Create a new row in the DataTable 
var colDataTable = CurrentTable.NewRow(); 

// Populate column "stringA" of the new row 
colDataTable[stringA]="some_string_B"; 

// Add the row to DataTable 
CurrentTable.Rows.Add(colDataTable); 

// Finally bind DataGrid to DataTable 
datagrid.ItemsSource = CurrentTable.AsDataView(); 

XAML

<DataGrid x:Name="datagrid" ItemsSource="{Binding CurrentTable}" CanUserAddRows="True" /> 

TetBox에 여러 가지 방법으로 열 스타일을 변경하려고했는데, 아마도 내가 오해 한 것일 수 있습니다.

답변

0

당신이해야 당신의 TextBox 스타일에 편집 ElementStyle 속성 :

dgCol.EditingElementStyle = new Style(typeof(TextBox)); 

DataGridTextColumn는 두 가지 스타일이있다. 하나는 표시 용이고 다른 하나는 편집 용입니다.

+0

Perfect! 고마워요! – Boltzmann