0

I need help trying to understand why this is not working. According to MSDN, TemplateBinding is what should be used when binding the property of a control in a template to a property of the control implementing the template.

Except that Template Binding is not two-way. For two-way you need to use binding and then specify the relative source as TemplatedParent.

So I have the following XAML: template

<ItemContainerTemplate x:Key="colHeaderTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding}" VerticalAlignment="Center"/>
                <ToggleButton Style="{StaticResource ToggleButtonStyle}" IsChecked="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Mode=TwoWay, Path=(props:VisibilityHelper.IsGroupCollapsed)}"/>
            </StackPanel>
        </ItemContainerTemplate>

which is used here

<dxg:GridColumn x:Name="Total" Header="Total" FieldName="field1" Width="Auto" HorizontalHeaderContentAlignment="Center" props:VisibilityHelper.IsGroupCollapsed="False" HeaderTemplate="{StaticResource colHeaderTemplate}">
                                        <dxg:GridColumn.EditSettings>
                                            <dx:TextEditSettings HorizontalContentAlignment="Center"/>
                                        </dxg:GridColumn.EditSettings>
                                    </dxg:GridColumn>

The toggle button in the template must set a dependency property on the grid column. This works fine when the template is binding to a parent ie. the controls are nested,

I just can't figure out what I am doing wrong.

MSDN ref - http://msdn.microsoft.com/en-us/library/ms742882.aspx

One of the many SO posts about this - In WPF, why doesn't TemplateBinding work where Binding does?

Thank you

dracosveen
  • 51
  • 5
  • I'm a bit rusty about WPF, but if I remember well the ItemContainer is (as the name suggests) the container of the cell, not the cell widget. That is, if you want to arrange a particular container for any kind of cell-content visualizer/editor. – Mario Vernari Jan 18 '22 at 05:16
  • 4
    `TemplateBinding` and `{RelativeSource TemplatedParent}` only work inside `ControlTemplate` and your template is not the one - it's `DataTemplate`. It looks like you need to set `RelativeSource` in your binding to `{RelativeSource FindAncestor, AncestorType={x:Type dxg:GridColumn}}` as long as you are trying to bind to property defined on column. – Quercus Jan 18 '22 at 05:21
  • Is ItemContainerTemplate the same as DataTemplate, because I am using ItemContainerTemplate? Either way I implemented what you suggested @Quercus but as expected I got the following `Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='DevExpress.Xpf.Grid.GridColumn', AncestorLevel='1''. BindingExpression:Path=(0); DataItem=null; target element is 'ToggleButton' (Name=''); target property is 'IsChecked' (type 'Nullable`1')` which I expected because FindAncestor only works for nested controls if I understand this correctly – dracosveen Jan 18 '22 at 21:26
  • @dracosveen `ItemContainerTemplate` is a descendant of `DataTemplate`. However, `FindAncestor` should work - you pass your template as `HeaderTemplate` for column, which means that when column is created it will use this template to create actual controls in header and in runtime `ToggleButton` will be nested control of column's header and hence of column itself. – Quercus Jan 19 '22 at 05:17

1 Answers1

0

Right so I have found the solution. Firstly DataTemplate does work. As @Quercus, it is all in the binding to the correct control.

In my case not the GridColumn but the GridColumnHeader. So this

IsChecked="{Binding RelativeSource={RelativeSource AncestorType=dxg:GridColumnHeader}, Path=DataContext.(props:VisibilityHelper.IsGroupCollapsed)}"

works perfectly...when bound to the correct parent.

Also as @Quercus stated, the template is actually nested and that is why this works. I used a tool called Snoop which actually shows you the visual tree of the application and then the datacontext of the selected element. Using this I solved this issue as well as 2 others I was having.

I really hope this helps someone somewhere before everyone goes to MAUI or WinUI 3.

dracosveen
  • 51
  • 5