Binding Visibility of a DataGridTemplateColumn to a dependency property in XAML

Mar 19, 2009 at 10:38 PM
I have a WPF Usercontrol that has a WPF toolkit datagrid.  The usercontrol exposes a dependency property of type visibility

In XAML, I am trying to bind the Visibility property on a DataGridTemplateColumn to the dependency property. However, it cannot seem to find it.  Can anyone help? I have been banging my head on this one for several hours...


<tk:DataGridTemplateColumn Header="Account" Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=CanSupportAccount, diagnostics:PresentationTraceSources.TraceLevel=High}" CellTemplate="{StaticResource AccountCellTemplate}"CellEditingTemplate="{StaticResource AccountCellTemplate}"/>

And in the codebehind:
public static DependencyProperty CanSupportAccountProperty;

Visibility CanSupportAccount{get{return (Visibility)GetValue(CanSupportAccountProperty);}
set{SetValue(CanSupportAccountProperty, value);}}


CanSupportAccountProperty =

DependencyProperty.Register("CanSupportAccount", typeof(Visibility),typeof(CheckpointsGrid));

I have also tried to use my control in the FindAncestor attribute of the binding statement i.e.


Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type SharedControls:CheckpointsGrid}}, Path=CanSupportAccount, diagnostics:PresentationTraceSources.TraceLevel=High}"

Anyway, I would greatly appreciate help! I admit defeat!
Thanks so much.



Mar 23, 2009 at 8:51 PM
Columns are not actually part of the visual tree and that's why it cannot find your UserControl in the binding.  There is a workaround to get the DataContext to the columns,  However, having the datacontext will not help if you are trying to bind to Visuals.  You may want to have that CanSupportAccount DP in a viewmodel instead of another UserControl.
Mar 23, 2009 at 10:11 PM
First of all, thank you! I am still quite new to this. I do understand your answer. Based on this information, is it impossible to do the following:
1. I have a DataTemplate that contains a ComboBox for my DataGridTemplateColumn.
2. The DataContext (which has my instance of my Controller) is inherited from the parent and used so that the ComboBox's SelectedValue={Binding Path=Routing.Type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged} can function. It does so beautifully.

The ItemsSource is from an XMLDataProvider. However, the XPath varies (this is a reusable control). Therefore, I thought I could create a dependency property, i.e. RoutingTypeXPath and reset the binding in the code behind when I get notification that the dependency property has

The combo box in the datatemplate has the following for binding
ItemsSource="{Binding Source={StaticResource XMLRoutingDS}, Mode=OneWay, Path=RoutingTypeXPath, UpdateSourceTrigger=PropertyChanged}"

And in a non-static method after my event in the code behind fires when the DP changes, I basically do a::

XMLDataProvider xmlDP=TryFindResource("XmlRoutingData") as XMLDataProvider;
if (xmlDP != null)
    xmlDP.Source = new URI (etc);
   xmlDP.XPath = RoutingTypeXPath;

It looks like good code to me. :-)....I also tried to create a new binding for the combobox,  but because it is in a datatemplate, I do not think I can get to it.  And, as I said,  I cannot use the datacontext because that is already used.

Would greatly appeciate your help (again). THANKS!