1

I am using telerik radgridviews and I have some columns that I want to show only if a checkbox is checked.The checkbox is outside the grid and the columns I want to show/hide are inside another grid. This is what I have tried, but doesn't seem to work:

<Grid>
<Checkbox Name = "test">
</Grid>
<telerik:RadGridView ...>
     <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn>
         <telerik:GridViewDataColumn.CellTemplate>
             <DataTemplate>
                  <telerik:RadGridView ...                                
                    <telerik:RadGridView.Columns>
                       <telerik:GridViewDataColumn IsVisible="{Binding IsChecked,Source={x:Reference test}, Converter{StaticResource BooleanToVis}}" />                                                                                                                                
         </telerik:RadGridView.Columns>
                    </telerik:RadGridView>
              </DataTemplate>
             </telerik:GridViewDataColumn.CellTemplate>

Update #2 So I have the below, but it doesn't seem to work .I dont get any binding issues but checking or unchecking the checkbox doesnt do anything.

 public bool IsChecked
    {
        get
        {
            return isChecked;
        }
        set
        {
            if (isChecked != value)
            {
                isChecked = value;
                NotifyPropertyChanged(nameof(IsChecked));
            }
        }
    }

<BooleanToVisibilityConverter x:Key="BooleanToVis"/>         
<CheckBox x:Name="showCols" Content="test" IsChecked="{Binding IsChecked}"/>                                             
<telerik:GridViewDataColumn DataMemberBinding="{Binding x}" IsVisible="{Binding DataContext.IsChecked,Source={x:Reference showCols},Converter={StaticResource BooleanToVis}}" />
user1855165
  • 289
  • 1
  • 8
  • 22

2 Answers2

2

If i understood you correctly you are looking to change the visibility of a GridViewDataColumn based on a checkbox, there are basically two ways to do that:

  • The easy and straightforward way to do this is simply by binding the column's IsVisible property to the CheckBox's IsChecked porperty using elementname:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <CheckBox Name="ChkBox" />
        </Grid>
        <telerik:RadGridView  Grid.Row="1">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn IsVisible="{Binding Path=IsChecked, ElementName=ChkBox}"  Header="First Name" UniqueName="FirstName" />
                <telerik:GridViewDataColumn  Header="Last Name" UniqueName="LasttName" />
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
    
  • The other way is to bind both the IsChecked dependency property of the checkbox and the IsVisible of the GridViewDataColumn to the same property:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <CheckBox Name="ChkBox" IsChecked="{Binding IsCheckedP}"/>
        </Grid>
        <telerik:RadGridView  Grid.Row="1">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn IsVisible="{Binding IsCheckedP}"  Header="First Name" UniqueName="FirstName" />
                <telerik:GridViewDataColumn  Header="Last Name" UniqueName="LasttName" />
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
    
    
      private bool _isChecked;
        public bool IsCheckedP
        {
            get { return _isChecked; }
            set
            {
                _isChecked = value;
                OnPropertyChanged();
            }
        }
    
        public ViewModel()
        {
            //...
        }
    
        public event PropertyChangedEventHandler PropertyChanged;          
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    

Make sure to implement the INotifyPropertyChanged interface and to properly set the DataContext.

SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47
0

try this (source here )

<FrameworkElement x:Name="dummyElement"  Visibility="Collapsed"  />
<DataGrid .... >
    <DataGrid.Columns>
        <DataGridTextColumn Visibility="{Binding DataContext.IsChecked, 
                                                 Source={x:Reference dummyElement}, 
                                                 Converter={StaticResource BooleanToVisibilityConverter}}"  />
    </DataGrid.Columns>
</DataGrid>

change your Name="test" for x:Name="test" (difference here)

Celso Lívero
  • 716
  • 2
  • 14
  • 18