in my WPF project, I try to create the displayed objects in Code behind. For some properties I use Element Binding, f.e.:
<GridViewColumn ... Width="{Binding ElementName=dummywidth1, Path=ActualWidth}" >
To do this in code behind, I use the following code:
GridViewColumn column = new GridViewColumn();
Binding binding = new Binding("ActualWidth");
binding.ElementName = "dummywidth1";
BindingOperations.SetBinding(column, GridViewColumn.WidthProperty, binding);
where "dummywidth1" is a Border with the width to use.
This works well, if the Border "dummywidth1" is already part of the .xaml file. If I create the Border in code behind as well, the width is NOT applied to the column.
Border border = new Border();
border.Name = "WidthHelper" + Grid_Sizer.ColumnDefinitions.Count;
Grid_Sizer.Children.Add(border);
and use the new border.Name for the binding.
binding.ElementName = border.Name;
It seems, that the Element Binding does not work with the just created Border object. When the WPF is displayed, the borders created in code behind have the desired size, but that size is not transfered to the column. Is there any way to achieve the element binding with both elements created in Code behind?
Thanks in advance,
Frank