5

I'm trying to implement XamDataGrid column visibilty in a MVVM architecture and it does not seems to be working.

I'm doing the following:

Adding Visiblility property for an unbound field -

<igDP:UnboundField Name="gridCustomerId" 
                   Label="ID" 
                   Binding="{Binding customerid, 
                                     Mode=TwoWay, 
                                     UpdateSourceTrigger=PropertyChanged}" 
                   Visibility="{Binding ShowCustomerIDColumn, 
                                        Mode=TwoWay, 
                                        UpdateSourceTrigger=PropertyChanged}">

In my View Model, adding a proerty of Visibility type:

//ToShow CustomerID Column
private Visibility showCustomerIDColumn; 
public Visibility ShowCustomerIDColumn 
{
    get
    {
        return showCustomerIDColumn; 
    }

    set
    {
        showCustomerIDColumn=value; 
        InvokePropertyChanged("ShowCustomerIDColumn"); 
    }    
}

Then in the command handler using the following code:

if(ShowCustomerIDColumn == Visibility.Hidden) 
    ShowCustomerIDColumn = Visibility.Visible; 
else
    ShowCustomerIDColumn = Visibility.Hidden; 

InvokePropertyChanged("ShowCustomerIDColumn");

Anybody with a solution?

Cheers, Anshuman

Jake1164
  • 12,291
  • 6
  • 47
  • 64
Anshuman
  • 577
  • 1
  • 8
  • 23

1 Answers1

3

The main reason why visibility is not working here cause Visibility is not a dependency property in Fields Class(UnboundField class is derived from Field class) unlike other properties.

See visibility property

See visibility property

See Label/RowSpan Property that support binding

See Label Property that support binding

You need to set the visibility in c# code(either in some behavior or code behind event):

fieldlayout.Fields[node.Name].Visibility = node.Visibility ? Visibility.Visible : Visibility.Collapsed;
Kylo Ren
  • 8,551
  • 6
  • 41
  • 66
  • more precise code for access: `myXamDataGrid.FieldLayouts[0].Fields["myFieldName"].Visibility = myVisibility` (note: sometimes the index can different than 0; note 2: you have define the `Name` property for the (Unound)Field) – Beauty Jan 18 '18 at 11:48