Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - help converting XAML binding expression to codebehind

Can anyone tell me what is the c# equivalent of the following snippet of XAML ??

<my:DataGridTextColumn 
                Visibility="{Binding Path=DataColumns[21].IsVisible, Source={StaticResource viewmodel}, Converter={StaticResource vc}}"                    
                Binding="{Binding SdDevDuration}"
                />

Its the visibility binding I cannot get right. DataGridTextColumn is not a FrameworkElement so no SetBinding method.

Thanks in advance,

Matt

like image 471
Matt Randle Avatar asked Jul 22 '09 10:07

Matt Randle


1 Answers

I worked this out. For anyone who's interested you can use the BindingOperation.SetBinding method.

The full code is,

var newCol = new DataGridTextColumn();
newCol.Binding = new Binding("SdDevDuration");

var visiblityBinding = new Binding("IsVisible");
visiblityBinding.Source = col;
visiblityBinding.Converter = new VisibilityConverter();                        
BindingOperations.SetBinding(newCol, DataGridTextColumn.VisibilityProperty, visiblityBinding);
like image 71
Matt Randle Avatar answered Sep 25 '22 17:09

Matt Randle