WPF では、いくつかの列が定義されたグリッドがあり、各列の幅は次のように DataGrid 列の幅にバインドされています。
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding ElementName=dataGrid, Path=RowHeaderWidth}" />
<ColumnDefinition Width="{Binding ElementName=Column0, Path=ActualWidth}" />
<ColumnDefinition Width="{Binding ElementName=Column1, Path=ActualWidth}" />
Etc.
<Controls:DataGrid BorderBrush="White" ItemsSource="{Binding DataTable}"
Name="datagrid1" Grid.Row="2" RowHeaderWidth="0">
<Controls:DataGrid.Columns>
<Controls:DataGridTextColumn Header="Included" Width="50" x:Name="Column0" />
<Controls:DataGridTextColumn Header="First" Width="100" x:Name="Column1" />
Etc.
プログラムを実行して手動で列のサイズを変更すると、Grid 列のサイズが変更され (ShowGridLines = true)、特定の Grid 列に関連付けられた要素が適切に移動することがわかります。
ただし、データ グリッドとグリッド列をコードに追加しようとすると、バインディングが機能しません (バインディング エラーは発生しません)。次に例を示します。
binding = new Binding()
{
Source = dataGrid.Columns[col],
Path = new PropertyPath("ActualWidth"),
Mode = BindingMode.OneWay,
};
colDef.SetBinding(WidthProperty, binding);
他のバリエーション (ElementName = "DataGridColumn1"、Path = new PropertyPath("ActualWidth") など) を試しましたが、エラーが発生しない (バインディングがない) か、「バインディングのソースが見つかりません」エラーまたは BindingExpression パス エラーが発生します。
コードでバインディングを設定する方法があるはずです...?