Search code examples
c#wpfbindingsyncfusion

SfDataGrid C# WPF - Binding on IsHidden don't update


I have SyncFusion at version 19.4.0.56 in C# WPF and my Binding works when I run my program (if I set the boolean to true, the column is hidden, if I set it to false, it is visible).

My problem is that when I update it. My SfDataGrid does not adapt.

Here is my code:

Properties bool

public bool UserExpertValues
{
    get => _userExpertValues;

    set
    {
        if (value != _userExpertValues)
        {
            SetProperty(ref _userExpertValues, value);
            _userExpertValues = value;
            RaisePropertyChanged(nameof(UserExpertValues));
        }
    }
}

Column (GridTextColumn)

<chart:GridTextColumn MappingName="{Binding ColumnsText[2], Converter={StaticResource TranslationConverter}}" IsHidden="{Binding UserExpertValues, Mode=TwoWay}">

I can ensure that the value of my boolean property is changed. It is changed in the ShowHideColumns() method.

true to false


Solution

  • By default, the MappingName is a String property that is not bindable property (Dependency property). In your scenario, you are trying to use this like a dependency property (bindable property) with the Converter, that’s why the column does not populate properly. So, you can define the underlying property name to the MappingName and if you want to change the header text then you can define the binding with converter for HeaderText property of GridColumn below mentioned code snippet,

    <syncfusion:GridTextColumn MappingName="Country"
                           HeaderText="{Binding Path=ColumnsText, Converter={StaticResource translationConverter}}" 
                           IsHidden="{Binding UserExpertValues, Mode=TwoWay, Source={StaticResource viewModel}}" />
    

    The column definitions of the DataGrid are not part of the SfDataGrid visual tree, so you can not bind SfDataGrid DataContext with GridColumn. However, you can overcome this problem by defining ViewModel (DataContext) inside Resources and you can bind ViewModel to GridColumns by using StaticResource binding as in the following code example.

    <Window.Resources>
     <local:TranslationConverter x:Key="translationConverter"/>
     <local:ViewModel x:Key="viewModel" />
     </Window.Resources>
     <Grid DataContext="{StaticResource viewModel}" >
          <Grid.ColumnDefinitions>
              <ColumnDefinition/>
         <ColumnDefinition Width="200"/>
     </Grid.ColumnDefinitions>
     <syncfusion:SfDataGrid x:Name="sfDataGrid"   
                            AllowEditing="True"
                            ItemsSource="{Binding Orders}"
                            AutoGenerateColumns="False">
         <syncfusion:SfDataGrid.Columns>
             <syncfusion:GridTextColumn MappingName="OrderID" HeaderText="Order ID" />
             <syncfusion:GridTextColumn MappingName="CustomerID" HeaderText="Customer ID" />
             <syncfusion:GridTextColumn MappingName="CustomerName" HeaderText="Customer Name" />
             <syncfusion:GridTextColumn MappingName="Country"
                                        HeaderText="{Binding Path=ColumnsText, Converter={StaticResource translationConverter}}" 
                                        IsHidden="{Binding UserExpertValues, Mode=TwoWay, Source={StaticResource viewModel}}" />
             <syncfusion:GridTextColumn MappingName="UnitPrice" HeaderText="Unit Price" />
         </syncfusion:SfDataGrid.Columns>
     </syncfusion:SfDataGrid>
    

    Sample demo Link