Binding in a DataGridTemplateColumn

May 27, 2009 at 6:25 PM

I have defined a DataTemplate that contains a checkbox for a datagridtemplate column.  In my DataTemplate.Triggers, I have a multi-binding and a multi-converter.

My understanding is, a datatemplate in a datagridtemplatecolumn cannot see my binding to my dependency property. Nor does it appear to see bindings to properties on other controls.

So is there any other way to bind it where I take into account a dependency property in code behind -- or a property on another element (that is outside of the datagridtemplatecolumn? 

My triggers affect the visibility of the checkbox. I've looked at using the LoadingRow event for the datagrid. Is there a way to do this at all?

I'd welcome advice...Even if it is it can't be done.

Thanks

Coordinator
May 30, 2009 at 3:09 PM

Could you supply some code snippets of the issue you describe above?

May 30, 2009 at 6:50 PM

Wow, would I be thrilled if you could solve this!!!! I have a WPF datagrid whose ItemsSource is a collection in my model: HostPR.PREvent.PRPoints

I have a datagridtemplatecolumn whose visibility is bound to a property in this collection -- which works properly. HostPR.PREvent.PRPoints[i].IsControlPoint (where i is the position that the datagrid row is on.

What I would like to do is be able to access my other dependency properties in code behind to add more data triggers (or perhaps a multidatatrigger). Ideally, I would also like to access properties off of the parent class to determine enabling/disabling, i.e. HostPR.PREvent.IsReady.

The issue is, from the datatemplate, it does not seem to be able to find my code behind because I believe that it cannot see the visual tree. So how do people use a datatrigger in a datatemplate when they want to affect the outcome by properties outside of the immediate datacontext of the datatemplate?  Is there a way to do this in code behind? Basically, I am looking to affect the enabling of a control in a datagrid cell by triggering off of either other control's properties or other dependency properties.

1) I have a WPF DataGrid:

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

<

</font></font><font size="2" color="#0000ff">

 

</font>

tk:DataGrid x:Name="PRRoutingDataGrid"

<font size="2" color="#ff0000"><font size="2" color="#ff0000">

ItemsSource

</font></font><font size="2" color="#ff0000">

 

</font>

="{Binding Path=HostPR.PREvent.PRPoints, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ..../>  This works

<font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font><font size="2" color="#a31515">

 

</font>

<tk:DataGridTemplateColumn Header="Set CT" x:Name="controlDate"   <font size="2">

 

</font>

CellTemplate="{StaticResource ControlTimeDateCellTemplate}"

<font size="2">

 

</font>

CellEditingTemplate="{StaticResource ControlTimeDateCellTemplate}"/>

<font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font><font size="2" color="#a31515">

 

</font>

<DataTemplate x:Key="ControlTimeDateCellTemplate" >

<font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font>
<font size="2" color="#a31515">

 

</font>

<CheckBox

<font size="2">

 

</font>

x:Name="controlCheckBox"

<font size="2">

 

</font>

IsChecked="{Binding Path=IsControlPoint, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"   -- works perfectly<font size="2">

 

</font>

Checked="HandleControlPointCheckbox" <font size="2">

 

</font>

Unchecked="HandleControlPointCheckbox" <font size="2">

 

</font>

VerticalAlignment="Center" /> <font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font><font size="2" color="#a31515">

 

</font>

<DataTemplate.Triggers>

<font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font>
<font size="2" color="#a31515">

 

</font>

<DataTrigger Binding="{Binding Path=prPointType}" Value="{x:Static PlanningData:PRPointType.Bar}">   -- works perfectly<font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font><font size="2" color="#a31515">

 

</font>

<Setter Property="Visibility" Value="Collapsed" TargetName="controlCheckBox"></Setter> <font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font><font size="2" color="#a31515">

 

</font>

</DataTrigger>

 

 

 

 

 

 -- The following one does not work. This is a dp in my code behind. I can bind other controls to the dp with this syntax, but not when I am within the datagrid datatemplate.

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type PR_Controls:PREventControl}}, Path=XYControlPoint}" Value="True"> 

 

<font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font>
<font size="2" color="#a31515">

 

</font>

<Setter Property="IsEnabled" Value="True" TargetName="controlCheckBox"></Setter>

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

<font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font><font size="2" color="#a31515">

 

</font>

</DataTrigger>

 

</font></font><font size="2" color="#0000ff">

 

</font>

 

<font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font>
<font size="2" color="#a31515">

 

</font>

 

 

-- it can't find an element in the visual tree either.  

<DataTrigger Binding="{Binding ElementName=xyTextBox, Path=IsEnabled}" Value="True}">  

 

<font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font><font size="2" color="#a31515">

 

</font>

<Setter Property="IsEnabled" Value="True" TargetName="controlCheckBox"></Setter>

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

 

 

<font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font><font size="2" color="#a31515">

 

</font>

</DataTrigger>

</font></font><font size="2" color="#0000ff">

 

</font>

 

</DataTemplate.Triggers

THANK YOU!!!!!!!!!!!!!! Solving this will solve a huge usability problem for us and render the datagrid SO much more usable.