GridViewDataColumn.IsVisible

58 posts, 0 answers
  1. Eric Zeng
    Eric Zeng avatar
    19 posts
    Member since:
    Aug 2009

    Posted 16 Mar 2010 Link to this post

    <Telerik:GridViewDataColumn Header="test" IsVisible=="{Binding Path=tName, Converter={StaticResource tNameToVisibilityConverter}}"  />


    When I using this way to decide whether displaying column or not, the error happened.
    But I have used this way in other places for many times, it only has problem when using on GridViewDataColumn .
    So I want to ask you, can we use this way here?



  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 16 Mar 2010 Link to this post

    Hi,

    You cannot bind column properties since Binding is supported only for FrameworkElement – grid columns are plain DependencyObjects.

    Sincerely yours,
    Vlad
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft banner
  4. Eric Zeng
    Eric Zeng avatar
    19 posts
    Member since:
    Aug 2009

    Posted 16 Mar 2010 Link to this post

    thanks:)
  5. Rodrigo Rodriguez
    Rodrigo Rodriguez avatar
    4 posts
    Member since:
    Dec 2009

    Posted 25 Jun 2010 Link to this post

    You can use this mechanism. In your ViewModel you would set ColumnsToHide to a string containing columns names separated by a comma.

    using System.Windows; 
     
    namespace Helpers 
        public class ColumnHiderBinder 
        { 
            public static readonly DependencyProperty ColumnListProperty = 
                DependencyProperty.RegisterAttached("ColumnList"typeof(string), 
                typeof(string), new PropertyMetadata(OnColumnListChanged)); 
     
            public static string GetColumnList(DependencyObject d) 
            { 
                return (string)d.GetValue(ColumnListProperty); 
            }         
             
            public static void SetColumnList(DependencyObject d, string value) 
            { 
                d.SetValue(ColumnListProperty, value); 
            }          
             
            private static void OnColumnListChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
            { 
                if (!(d is RadGridView)) return
     
                var obj = ((RadGridView) d); 
     
                foreach (var item in ((string) e.NewValue).Split(new [] {','})) 
                { 
                    obj.Columns[item].IsVisible = false
                } 
            } 
        } 
     


    And then in your XAML:

          
    <RadGridView  Helpers:ColumnHiderBinder.ColumnList="{Binding ColumnsToHide}" > 


  6. Kaka
    Kaka avatar
    1 posts
    Member since:
    Jun 2010

    Posted 30 Jun 2010 Link to this post

    Hello Vlad,
    I am currently trying out your WPF RadGrid control, and first of all I really have to say great job to you all. It's a really good product compared to all the others out there.
    However, I couldn't help it to notice your statement about DataBinding on DependencyObjects and I really cannot figure it out. Could you please be so kind as to helping me out on this one.
    First lets look at this post by Mr. Nikolay Atanasov. In it, Telerik proudly announces native Silverlight 4 support of it's controls. In the product list provided, there is your RadControls suit for SL4 (it's the last one with the red rectangle around it). The post is dated to March 18, 2010. Your own post, in which you say that there is no DataBinding on DependencyObjects is dated to March 16, 2010. I am guessing that 2 days is not such a long time to turn things around so dramatically. Having said that, I am guessing that you probably had native support for SL4 long before the announcement day on March 18 (please correct me if I'm wrong).
    By now your statement has been that your RadControls have native support for SL4, and that DataBinding is not possible on DependencyObjects. To be honest I'm really confused here. A long time ago I've read this post by Mr. Shawn Wildermuth about some of the changes in SL4. As he says, DataBinding is actually now available on DependencyObjects. When I read your post Vlad, I sat down and opened Visual Studio. Started out a new project and as it turns out, in SL4 the Binding has really moved out to the DependencyObject contrary to SL3 in which it really was in the FrameworkElement.
    My question would be as follows: How can you say your RadControls have native SL4 support, when you guys don't even know the difference between SL3 and SL4?
  7. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 30 Jun 2010 Link to this post

    Hello Kaka,

    I am sure that Vladimir Enchev had Silverlight 3 in mind when he made that comment. Binding on DependecyObject is indeed supported in Silverlight 4.

    Excuse us for the misunderstanding. 


    Kind regards,
    Milan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. Juan
    Juan avatar
    13 posts
    Member since:
    Jun 2010

    Posted 26 Jul 2010 Link to this post

    Can anybody give me an example how to use visibility binding in RadGridView ? I need to hide some rows in it.
  9. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 26 Jul 2010 Link to this post

    Hello Juan,

     This thread is related to columns visibility - can you elaborate a bit more on your scenario? Generally messing with rows visibility is not recommended since the grid virtualization will use it and you may get problems with such approach. It will be better if you filter your items before binding the grid or use the grid filtering to hide some of them.

    Regards,
    Vlad
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  10. Jaroslav Půbal
    Jaroslav Půbal avatar
    52 posts
    Member since:
    Dec 2009

    Posted 26 Aug 2010 Link to this post

    So back to the top question.
    Why dont work databinding on GridViewDataColumn on IsVisible in Silverlight4.

     

     

     

    <Controls:GridViewDataColumn DataMemberBinding="{Binding Rusky}" Header="Russian" IsReadOnly="True" IsVisible="{Binding ColumnRuskyVisible}"/>

     

  11. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 30 Aug 2010 Link to this post

    Hi Jaroslav Půbal,

    You can easily use a Binding to the IsVisible property of the column. I am sending you a sample project illustrating that functionality. Following the scenario in the sample application, once you check the CheckBox in the bottom, the visibility of the second column will be changed.
    I hope that helps.
     

    Sincerely yours,
    Maya
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  12. Rodrigo Rodriguez
    Rodrigo Rodriguez avatar
    4 posts
    Member since:
    Dec 2009

    Posted 30 Aug 2010 Link to this post

    Maya, this is a new feature, right? I have noticed in the updated Telerik assemblies. So the ColumnHiderBinder is not necessary anymore.
  13. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 31 Aug 2010 Link to this post

    Hello,

     This is now supported by the Silverlight (4) framework itself (unlike Silverlight 3) - you can remove old hacks. 

    Regards,
    Vlad
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  14. Jaroslav Půbal
    Jaroslav Půbal avatar
    52 posts
    Member since:
    Dec 2009

    Posted 31 Aug 2010 Link to this post

    Hi Maya,
    I finally figured out why column visibility does not work and how to simulate.
    Dont know if it Telerik or SL4 bug.

    Based on your attachment I change in MainPage.xaml two lines.

     

    <telerik:GridViewDataColumn IsVisible="{Binding IsChecked, ElementName=CheckBox1}" ... 

     

    <CheckBox x:Name="CheckBox1" />

     

    1. Now I expected that after application start CheckBox is unchecked and column is invisible. But column is visible.
    2. When checking and unchecking I expect column is hiding and showing. But nothing happens unless I select column with mouse.

    MainPage.xaml

    <UserControl x:Class="BindingToIsVisible.MainPage"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                 xmlns:my="clr-namespace:BindingToIsVisible"
                 mc:Ignorable="d" d:DesignHeight="700" d:DesignWidth="700">
        <UserControl.Resources>
            <my:MyViewModel x:Key="MyViewModel"/>       
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" 
              Background="White" 
              DataContext="{StaticResource MyViewModel}">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
             
            <telerik:RadGridView Grid.Row="0" 
                                 Name="clubsGrid" 
                                 ItemsSource="{Binding Clubs}"
                                 AutoGenerateColumns="False"                            
                                 Margin="5">
                <telerik:RadGridView.Columns>               
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Established}"
                                                Header="Est." 
                                                IsVisible="{Binding IsChecked,  ElementName=CheckBox1}"
                                                DataFormatString="{}{0:yyyy}"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding StadiumCapacity}" 
                                                Header="Stadium" 
                                                DataFormatString="{}{0:N0}"/>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
              
            <StackPanel Grid.Row="1" Orientation="Horizontal">
                <TextBox Text="Click here to hide the last column" />
                <CheckBox x:Name="CheckBox1"  />
            </StackPanel>
        </Grid>
    </UserControl>
  15. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 02 Sep 2010 Link to this post

    Hi Jaroslav Půbal,

    Following up your scenario, you need to set the Binding as follows:

    <CheckBox x:Name="CheckBox1" IsChecked="{Binding Columns[2].IsVisible,  
                                          ElementName=clubsGrid, Mode=TwoWay}"
    />


    Regards,
    Maya
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  16. Jaroslav Půbal
    Jaroslav Půbal avatar
    52 posts
    Member since:
    Dec 2009

    Posted 02 Sep 2010 Link to this post

    Hi Maya,
    thank you for answer.
    You solution works great. Except one case. When I need show/hide two columns.

    <telerik:GridViewDataColumn Header="system column 1" IsVisible="{Binding IsChecked, ElementName=CheckBox1}" ... 

    <telerik:GridViewDataColumn Header="system column 2" IsVisible="{Binding IsChecked, ElementName=CheckBox1}" ... 

    <CheckBox x:Name="CheckBox1" Header="System columns visible"/>



    Is this behaviour expected? Will you fix this bug or this is by design right behaviour.
    Thank for answer.
  17. Jarred Froman
    Jarred Froman avatar
    57 posts
    Member since:
    Aug 2008

    Posted 02 Sep 2010 Link to this post

    How would you do this via code-behind?  Normally, you could use the SetBinding function, but since the GridViewDataColumn doesn't inherit from the FrameworkElement, this function isn't available.

    Thanks,
    -Jarred Froman
  18. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 07 Sep 2010 Link to this post

    Hello Jaroslav Půbal,

    On examining your requirements, another - and probably the most appropriate solution for that case - came up. What needs to be done here is to set the "Source" Property on defining the Binding of the IsVisible Property of the column. Thus once you set your ViewModel as a Resource, you will be able to use it afterwards like follows:

    <UserControl.Resources>
          <my:MyViewModel x:Key="MyViewModel"/>    
    </UserControl.Resources>
     
    <telerik:RadGridView.Columns>              
          <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
          <telerik:GridViewDataColumn DataMemberBinding="{Binding Established}"
    Header="Est."
                       IsVisible="{Binding IsColumnVisible, Source={StaticResource MyViewModel}}"                                                                                   
                     
    DataFormatString="{}{0:yyyy}"/>
    <telerik:GridViewDataColumn DataMemberBinding="{Binding StadiumCapacity}"
    Header="Stadium"
                       IsVisible="{Binding IsColumnVisible, Source={StaticResource MyViewModel}}"
    DataFormatString="{}{0:N0}"/>
    </telerik:RadGridView.Columns>

    <CheckBox x:Name="CheckBox1"
            IsChecked="{Binding IsColumnVisible, Source={StaticResource MyViewModel},         Mode=TwoWay}"/>

    Where "IsColumnVisible" is a property defined in the ViewModel.
    Thus both columns as well as the CheckBox are aware where exactly to find that property - the MyViewModel.
    I am sending you a sample project illustrating the proposed solution.


    Regards,
    Maya
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  19. Jaroslav Půbal
    Jaroslav Půbal avatar
    52 posts
    Member since:
    Dec 2009

    Posted 07 Sep 2010 Link to this post

    Thank you for your interest and sample code.

    I already discover this solution. It is little strange that ElementName= no working, but it doesnt matter.
    I add extra property on ViewModel even I never touch it from ViewModel code.

    Maybe this is SilverLight 4 bug :-)

    Thank you for superb support!
  20. Sam
    Sam avatar
    6 posts
    Member since:
    Oct 2010

    Posted 11 Oct 2010 Link to this post

    Maya and Vlad,

    I am having the same exact problem as Jaroslav, where a binding on GridViewColumn.IsVisible is only triggered when a cell within that column is selected.  I am currently using Q2 2010 (no service packs), and I need to know how to combat the problem.  Has telerik been able to reproduce this issue?  Has it been fixed in recent service packs?

    Looking forward to your response,
    Sam
  21. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 14 Oct 2010 Link to this post

    Hello Sam,

    The fact that the ElementName is not working as expected in this case is due to some limitations in the Framework. Is it not appropriate for you to use the approach with setting the Source property of the Binding ? Please, share more details about your requirements, so that I could provide you with a proper solution.
     

    Regards,
    Maya
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  22. Sam
    Sam avatar
    6 posts
    Member since:
    Oct 2010

    Posted 14 Oct 2010 Link to this post

    The parent page has it's datacontext to a viewmodel that is set by a viewmodellocator.  Therefore, it is not a static resource.  The grid on the page does not have a datacontext, so therefore any data bindings on grid (and gridviewcolumn) properties should be bound to the view model on page.datacontext.

    Yet when i setup a binding on IsVisible, the property is not honored *until i click within a cell of that column*.  At that point, the databinding starts behaving normally.  This tells me two things:

    1. my binding is bound to the correct viewmodel property, because after clicking in the cell, it works as expected.
    2. this is not the elementname problem you speak of, because I am not using it here at all.  

    --Sam
  23. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 20 Oct 2010 Link to this post

    Hello Sam,

    Based on the previous post, I got the impression that you are experiencing the same issue as the others with setting the ElementName binding property. How exactly are you setting the binding to IsVisible Property of the column ? Do you change it by checking/ unchecking a CheckBox as mentioned here before ? Any relevant code-snippet would be helpful.
     

    Kind regards,
    Maya
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  24. Alexey Oyun
    Alexey Oyun avatar
    74 posts
    Member since:
    Mar 2010

    Posted 29 Oct 2010 Link to this post

    Hi,

    If you change example solution you have provided, and change bindning in checkbox to
    <telerik:GridViewDataColumn DataMemberBinding="{Binding Established}"
                                       Header="Est."
                                       IsVisible="{Binding ElementName=LayoutRoot, Path=DataContext.IsColumnVisible}"
                                       DataFormatString="{}{0:yyyy}"/>

    Binding won't work, right now I have created resource just to be able to hide column
    :(

    Alexey
  25. kulashaker
    kulashaker avatar
    5 posts
    Member since:
    Jul 2006

    Posted 02 Nov 2010 Link to this post

    I am having the same problem:

    When binding "IsVisible" property in the following way:

    <telerik:GridViewDataColumn
    Header="Pecent"  
    DataMemberBinding="{Binding PercentDouble}"  
    IsVisible="{Binding Path=DataContext.ShowPercentageColumn, ElementName=LayoutRoot, Mode=OneWay}" />


    It does not work unless the user has clicked in the Column once. After the user clicks on the column, it work nicely.

    Has anybody found a solution to this problem?

    Thanks
    Bruce
  26. Devon
    Devon avatar
    11 posts
    Member since:
    Jun 2008

    Posted 04 Nov 2010 Link to this post

    Try making the binding Two way:

    IsVisible="{Binding Path=DataContext.ShowPercentageColumn, ElementName=LayoutRoot, Mode=TwoWay}"

    This should fix the problem.

    If you don't have any binding Mode Like :
    IsVisible="{Binding Path=DataContext.ShowPercentageColumn, ElementName=LayoutRoot}"

    then it will use the default binding mode for that property, I assume that IsVisible's default binding mode is OneWay, which is why it is not working for that scenario either.

    If you want to programatically change the value of a bound value and have the UI update itself automatically (without any user interaction on the UI), then you need to make the Mode=TwoWay. 

    Hope this helps.
  27. kulashaker
    kulashaker avatar
    5 posts
    Member since:
    Jul 2006

    Posted 04 Nov 2010 Link to this post

    Hi Devon, thanks for your reply, but it did not fix the problem. It reacts the same for both OneWay and TwoWay modes. User must click on the column for it to work as expected.
  28. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 08 Nov 2010 Link to this post

    Hello kulashaker,

    Using the ElementName binding is not as reliable as expected and unfortunately it does not always provide the desired result. One possible approach would be to define a Resource and use it as a StaticResource in the Binding as demonstrated above. 
    Another technique is to wrap the RadGridView in a custom UserControls that exposes a property responsible for the column's visibility. I am sending you a sample project illustrating the proposed solution.
     

    Kind regards,
    Maya
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  29. Dave Clark
    Dave Clark avatar
    1 posts
    Member since:
    Jun 2010

    Posted 04 Jan 2011 Link to this post

    I have the same problem as Sam, was this ever resolved ?.

    Basically I am binding to a viewmodel, on that viewmodel is a bool property which i bind to IsVisible property on the GridViewDataColumn.
    This bool property can change, depending on what mode the grid is in, and hence columns should hide/show depending on this.
    When the datagrid is first shown, the binding is not initiated and only gets initiated when I click in the column.
    I know this because I can change the binding to an incorrect property and I get a binding error in debug output (only when i click in the column), if I don't click in the column, i get no binding error, which tells me that the binding is not getting initialized until you click in the column.

    Is there any solution to this ?
    I want columns to be hidden/shown on first entry, clicking in the column to fire bindings is just ..... well ....wrong.

    Many thanks

    Dave
  30. Geoff
    Geoff avatar
    1 posts
    Member since:
    Sep 2008

    Posted 19 Jan 2011 Link to this post

    Same problem here. I have a viewmodel that is the datacontext for the UserControl that the RadGridView is located in, and a bool property for each column. In the XAML, the IsVisible property on each of these columns is bound to the aforementioned properties in the viewmodel.

    I have also noticed that clicking the cells seems to "activate" the binding, and after that point the column shows or hides appropriately based on the value of the property.

    This seems like a bug to me. We were using the Microsoft DataGrid which didn't allow binding the Visibility property, so I was hoping that when we made the change to use the RadGridView, we would no longer have to set it programmatically.

    Thanks,
    Geoff
  31. Dave Swersky
    Dave Swersky avatar
    5 posts
    Member since:
    Apr 2010

    Posted 23 Jan 2011 Link to this post

    I'm also having this problem.  I can't use a static resource because I'm using a viewmodellocator.  I will need to show/hide columns in many places throughout the application I'm working on.  I hope there is a clean workaround that doesn't require a static context resource.
Back to Top
DevCraft banner