0
votes

I am trying to bind the visibility property of a DataGridTextColumn in WPF using code. Specifically, I need to bind it to the datacontext of a framework element.

 <FrameworkElement x:Name="DataContextControl"/>

In Xaml, I would do :

<DataGridTextColumn Visibility="{Binding DataContext, Source = {x:Reference DataContextControl}}">

I am building the columns dynamically so that's not an option here. How do I do the same binding programmatically since DataGridTextColumn doesn't have a setbinding command?

I have tried using SetValue to no avail.

1
No, that refers to the binding property on the column, but I need to be able to bind to the visibility property. Thanks though!qscott86

1 Answers

0
votes

Via BindingOperations:

var column = new DataGridTextColumn();
BindingOperations.SetBinding(
    column,
    DataGridColumn.VisibilityProperty,
    new Binding(...));