1
votes

I am trying to bind the visibility of columns in an Xceed datagrid to the IsChecked value of a checkbox.

<xcdg:DataGridControl ReadOnly="{Binding ElementName=ShowReferenceColumn, Path=IsChecked}">
    <xcdg:DataGridControl.Columns>
        <xcdg:Column FieldName="Reference" Visible="{Binding ElementName=ShowReferenceColumn, Path=IsChecked}" />
    </xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>

The ReadOnly property of the datagrid is getting updated by the chaning of the checkbox IsChecked value, but the visibility of the column is not changing. Am I missing something in the binding on the column?

Edit:

The ReadOnly property here is an example of a property where I was able to get the binding to work. In reality it is not going to be binding to the same checkbox as used for the column visibility.

2

2 Answers

3
votes

Try this:

<xcdg:Column FieldName="Reference" 
             Visible="{Binding RelativeSource={RelativeSource Self}, Path=DataGridControl.ReadOnly}" />

Edit:

The ReadOnly property here is an example of a property where I was able to get the binding to work. In reality it is not going to be binding to the same checkbox as used for the column visibility.

Then you need to bind the IsChecked property of the CheckBox to a source property of the view model and then bind the Visible property of the column to the same source property:

<xcdg:Column FieldName="Reference" 
             Visible="{Binding RelativeSource={RelativeSource Self}, Path=DataGridControl.DataContext.BooleanSourceProperty}" />

You cannot use ElementName in this context as the column and the CheckBox don't belong to the same namescope.

0
votes

Also met similar issue recently.

You can just utilize the Visible property, use the following generic approach and which is simple to understood:

<xcdg:ColumnFieldName="Reference" Title="Reference" 
Visible="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type typeOfAncestor}}, Path=DataGridControl.DataContext.BooleanSourceProperty}"/>

For example, if the typeOfAncestor is xcdg:MergedColumn and BooleanSourceProperty is IsChecked, then the code should be:

<xcdg:ColumnFieldName="Reference" Title="Reference"
Visible="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type xcdg:MergedColumn}}, Path=DataGridControl.DataContext.IsChecked}"/>

Then the issue can be solved, sometimes if there is an exception "Collection was modified; enumeration operation may not execute.", the exception also can be avoided easily.

Reference: https://xceed.com/forums/topic/Column-visible-binding-issue-MVVM/