ComboBoxまたはTextBlockとして列スタイルを持つDataGridに一連のデータを表示する必要があります。 DataGridはDataTableにバインドされています。 DataTableの各列の数と位置は実行時に定義されているため、プログラムでDataGridを作成します。 DataGridTextColumnをTextBoxスタイルに変更しようとすると、タイプエラーが発生しますが、DataGridTextColumnを使用してデフォルトのスタイル(TextBlock)を使用している限り、すべて問題ありません。 は、コンボボックスに関する何のためには問題ありませんので、私は(単一DataGridのセルのために)私のコードの以下のみDataGridTextColumnの一部を貼り付けます。WPF DataGridとDataTableおよび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にさまざまな方法で列スタイルを変更しようとしましたが、おそらく私は何か誤解していました。
完璧!どうもありがとうございました! – Boltzmann