GridViewDataColumn.IsVisible

58 posts, 0 answers
  1. Terry Foster
    Terry Foster avatar
    60 posts
    Member since:
    Sep 2009

    Posted 08 Mar 2011 Link to this post

    I just found and read through this entire thread, as I am desiring and expecting similar behavior as many here.  I noted how at no point is Telerik taking any responsibility for this issue - only workarounds.  And now more than a month has passed with no response.  What is the deal?  I, too, cannot define a static resource as I'm using a 'view model locator' concept as well.  And we shouldn't have to!  Has a bug report or feature request been opened for this?  I'm sure many here would quickly vote for this.

    Terry
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 15 Mar 2011 Link to this post

    Hi Terry Foster,

    Columns in RadGridView for Silverlight   are plain dependency objects  rather than visual elements. Altering the behavior of RadGridView  so that such bindings become possible would inevitably introduce a lot of breaking changes and inconsistent  behavior with previous versions making upgrades to newer versions a pain for lots of our customers. This is the reason we recommend the solutions / workarounds mentioned before.

    Regards,
    Pavel Pavlov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. John
    John avatar
    6 posts
    Member since:
    Feb 2011

    Posted 17 Mar 2011 Link to this post

    I'm currently using the 2010Q3 Silverlight controls. The following workaround worked for me:

            private void RadGridView_Loaded(object sender, RoutedEventArgs e)
            {
                var gridView = (GridViewDataControl)sender;
     
                var originalCurrentColumn = gridView.CurrentColumn;
                foreach (var column in gridView.Columns)
                    gridView.CurrentColumn = column;
                gridView.CurrentColumn = originalCurrentColumn;
            }
    

    It does seem that Telerik could make this happen by default, if they haven't already fixed it in the latest version.
  4. Brian
    Brian avatar
    73 posts
    Member since:
    Sep 2010

    Posted 06 May 2011 Link to this post

    So, binding the IsVisible property to a property in a view model is not possible?

    This seems like a MAJOR oversight in the control.
  5. Iurii
    Iurii avatar
    18 posts
    Member since:
    Oct 2006

    Posted 18 May 2011 Link to this post

    I completely agree. It defies the purpose of using XAML.
  6. John
    John avatar
    6 posts
    Member since:
    Feb 2011

    Posted 18 May 2011 Link to this post

    I totally agree also. However, notice that in my previous response, I have a very simple and general workaround that does allow you to bind the IsVisible property to a property in a view model.
  7. Brian
    Brian avatar
    73 posts
    Member since:
    Sep 2010

    Posted 19 May 2011 Link to this post

    John-

    Are you referring to your post on March 17th?  I don't see anything in that post regarding binding to IsVisible but if you are just saying that this could be done in combination with the code-behind, then I understand.

    Tks.
  8. John
    John avatar
    6 posts
    Member since:
    Feb 2011

    Posted 19 May 2011 Link to this post

    Correct. In the version of the RadControls (2010Q3sp1) and Silverlight (4) that I was using, I was able to bind to the IsVisible property of the <telerik:GridViewDataColumn>. However, the bindings didn't "take effect" until the user clicked on one of the columns (or something similar). The code-behind I provided just helped the Telerik control to do the right thing upon loading.
  9. Brian
    Brian avatar
    73 posts
    Member since:
    Sep 2010

    Posted 19 May 2011 Link to this post

    Ah, ok.  I'll give it a shot.  I have had no luck binding the IsVisible property to my view model so I'm skeptical but it's worth giving it a shot.

    Tks.
  10. Prakash
    Prakash avatar
    9 posts
    Member since:
    Jul 2009

    Posted 22 May 2011 Link to this post

    We had a similar problem the RadTreeListView (based on the RadGridView) when binding a column's GridViewDataColumn.IsVisible to a property in the ViewModel. A workaround was to create a subclass of the RadTreeListView, add a dependency property that in turn was bound to the ViewModel's property, and use the dependency property's property change callback to programmatically update the column's IsVisible property.

    BTW, setting a column's IsVisible to False within the xaml to initially hide the column was not working correctly. So we had to remove this from the xaml and programmatically set the IsVisible value to false within an override of OnApplyTemplate in the treeview subclass.


    Combining the two is working great for us.

    public class CustomTreeListView : RadTreeListView
        {
            public CustomTreeListView() : base()
            {
            }
      
            #region DTDColumnVisible Dependency Property
      
            /// <summary>
            /// Dependency property used for changing the DTDColumn visibility. Binding directly to GridViewDataColumn.IsVisible 
            /// property does not work. Workaround.
            /// </summary>
            public bool DTDColumnVisible
            {
                get { return (bool)this.GetValue(DTDColumnVisibleProperty); }
      
                set { SetValue(DTDColumnVisibleProperty, value); }
            }
      
            public static readonly DependencyProperty DTDColumnVisibleProperty = DependencyProperty.Register(
                "DTDColumnVisible", typeof(bool), typeof(CustomTreeListView), new PropertyMetadata(false, new PropertyChangedCallback(OnDTDColumnVisibleChanged)));
      
            private static void OnDTDColumnVisibleChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
            {
                // Update the DTDColumn visibility
                CustomTreeListView treelistview = obj as CustomTreeListView;
                if (treelistview != null)
                    treelistview.Columns["DTDColumn"].IsVisible = (bool)args.NewValue;
            }
      
            #endregion DTDColumnVisible Dependency Property
      
            public override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
      
                this.Columns["DTDColumn"].IsVisible = false;
            }
        }
  11. Paul McNamara
    Paul McNamara avatar
    30 posts
    Member since:
    Apr 2010

    Posted 21 Jun 2011 Link to this post

    I'm struggling with this too but I'm creating my columns programmatically and the bindings too.

    I have the latest controls and it doesn't seem to be picking up the bindings for IsVisibleProperty, IsReadOnlyProperty or HeaderProperty at all. I've tried hooking into the Loaded event like John suggested but it doesn't seem to make a difference. I'm using BindingOperations.SetBinding() to set the bindings on the GridViewDataColumn instance.

    I've noticed that there is an IsReadOnlyBinding which I can work with but I still need to be able to bind the visibility and header to my viewmodel. If it's just not possible I'll probably do something the the MVVM Lite messaging framework.
  12. Matthias
    Matthias avatar
    26 posts
    Member since:
    Apr 2011

    Posted 03 Jan 2012 Link to this post

    I absolutely agree that this is a major flaw!
    In WPF it works as expected, so why shall it be a breaking change to fix this BUG?

    edit: When I set IsVisible to False via ViewModel and click a cell within that column the column disappears and the program crashes...
  13. Kelly
    Kelly avatar
    73 posts
    Member since:
    Apr 2011

    Posted 13 Jan 2012 Link to this post

    I'm using V2011.3.1227.1040 of the gridview and SLR5. When I bind IsVisible to a view model, the binding is simply ignored. A definite answer how to make this work would be greatly appreciated.

    KO
  14. Tim
    Tim avatar
    1 posts
    Member since:
    Jun 2009

    Posted 13 Jan 2012 Link to this post

    Same problem for me also, but I have the complication of using the HierarchyChildTemplate and hiding columns in both the parent and child grid.  John's workaround worked for me (putting the Loaded event handler on both the parent and child grids), but still seems like the IsVisible should just work like other controls.

  15. JJ Loubser
    JJ Loubser avatar
    42 posts
    Member since:
    Jul 2009

    Posted 10 Feb 2012 Link to this post

    if the isVisible field is not working, take it out, do it for youre customers, please it is confusing...
  16. Juliana
    Juliana avatar
    71 posts
    Member since:
    Feb 2008

    Posted 24 Feb 2012 Link to this post

    Hi there,

    Could anybody help me to set binding for GridViewDataColumn.IsVisible property from code behind?

    Thanks
  17. Kelly
    Kelly avatar
    73 posts
    Member since:
    Apr 2011

    Posted 24 Feb 2012 Link to this post

    Hi Juliana,

    Here's what worked for me. The xaml binding is ignored, so I set bindings in the Loaded event for the page the grid is in. In my view model (based on INotifyPropertyChanged, naturally), I have one boolean property for each column in the grid, and the names of these properties correspond to the UniqueName fields for each column, for example, IsCol1Visible, IsCol2Visible, etc.

    In the loaded event I set the bindings like this:

    Binding b;
     
    foreach (GridViewBoundColumnBase c in grid.Columns)
    {
        b = new Binding(string.Format("Is{0}Visible", c.UniqueName));
        b.Mode = BindingMode.TwoWay;
        b.Source = View;
        BindingOperations.SetBinding(c, GridViewDataColumn.IsVisibleProperty, b);
    }

    I don't know if this is a good way, or if there is a better way, but this certainly works. Hope that helps!

    Kelly
  18. Juliana
    Juliana avatar
    71 posts
    Member since:
    Feb 2008

    Posted 27 Feb 2012 Link to this post

    Hi Kelly,
    Thanks a lot! This code works like a charm!
    Juliana
  19. guillekang
    guillekang avatar
    8 posts
    Member since:
    Oct 2006

    Posted 04 May 2012 Link to this post

      Hi.  I am currently using IsVisible property bound to a property of my view model.

    1.) Create a DataContextProxy class,  as in : 
    http://forums.silverlight.net/post/539919.aspx 

    i have used this proxy class in SL4 and SL5 and it works pretty well when using mvvm.

    2.) Add a resource to your user control:
    <myns:DataContextProxy x:Key="DCProxy/>

    3.) Bind the IsVisible property to a property of your view model:

      <telerik:GridViewDataColumn
                            Width="120"
                            DataMemberBinding="{Binding MyAdress, Mode=TwoWay}"
                            IsVisible="{Binding DataSource.IsMyAddressVisible, Source={StaticResource DCProxy}}">

    4.) Remeber to call PropertyChanged when IsMyAddressVisible  changes its value in your view model.

    Hope it helps,


    Guillermo
  20. John
    John avatar
    12 posts
    Member since:
    Jul 2011

    Posted 19 Jul 2012 Link to this post

    If you've upgraded to the latest silverlight you can go up the tree until you reach outside the grid and grab the data context that way.  

    IsVisible="{Binding Path=DataContext.IsColumnVisible, RelativeSource={RelativeSource FindAncestor, AncestorType=Border}}"
    
  21. Bob
    Bob avatar
    1 posts
    Member since:
    Aug 2012

    Posted 03 Aug 2012 Link to this post

    Hello,

    We are using the 2012.2.725.1050 build of SL controls. Is John's code behind workaround still the only way to get this to work. We require to bind this directly within the XAML without the need for code behind.  Any update?

    thx...vsdotnetguy
  22. Chethan
    Chethan avatar
    2 posts
    Member since:
    Oct 2012

    Posted 17 Oct 2012 Link to this post

    Hi Bob,
    Even we are using the same 2012.2.725.1050 build with SL5, and I am able to set the IsVisible property directly 
    from XAML and it works with no issues.

    Telerik Team,

    Binding from ViewModel to set IsVisible property of a GridVIewDataColumn doesn't work, 
    even the workarounds provided by many guys here to create a Static Resource doesn't work because, 
    we cannot create a parameterless constructor, without which I cannot access myViewModel class from XAML.

    Can you guys provide us with a solution??
    Thnx
    Chethan
  23. Tingting
    Tingting avatar
    14 posts
    Member since:
    May 2012

    Posted 12 Nov 2012 Link to this post

    Hello,   i have a problem with RadGridView. In my GridViewDataColumn, i cant use DataMemberBinding for binding with a another element (textbox1). The column TEST is always null.
    <TextBox Height="23" Name="textBox1" Width="120" Text="test"  />
     
     <telerik:RadGridView x:Name="dgrDetailsFacture"
          ItemsSource="{Binding ElementName=dgrFacture,Path=SelectedItem.GrpLigneFacture}"      
          IsSynchronizedWithCurrentItem="true"
         CanUserResizeColumns="False" >
                            <telerik:RadGridView.Columns>
                                 <telerik:GridViewDataColumn
                    TextAlignment="Center" IsReadOnly="True"
                                    DataMemberBinding="{Binding CodeArti, Mode=OneWay}"
                    Header="Article" HeaderTextAlignment="Center" Width="90" UniqueName="article"/>
                                <telerik:GridViewDataColumn
                                   TextAlignment="Left" IsReadOnly="True"
                                   DataMemberBinding="{Binding Mode=OneWay, ElementName=textBox1, Path=Text}"
                    Header="TEST" HeaderTextAlignment="Center" Width="*" />
         </telerik:RadGridView.Columns>
    </telerik:RadGridView>

     
  24. AMO
    AMO avatar
    31 posts
    Member since:
    Nov 2011

    Posted 14 Mar 2013 Link to this post

    thanx :)
  25. Scott Waye
    Scott Waye avatar
    122 posts
    Member since:
    Nov 2009

    Posted 23 Jan 2015 Link to this post

    This is poor.  IsVisible on GridViewColumn is a dependency property, therefore it should be bindable.  If a breakpoint is added to the binding in the XAML, then you can see that initially the datacontext is correct, but subsequently it gets set to something else and the binding evaluates to null which makes the column visible.  At the least you should be able to navigate to the IsVisible property in the object explorer and see a comment explaining that it cannot be used for binding.

    In my case I have the grid in a user control which exposes a dependency property that that view model can bind to.  The workaround of John's with the loaded event did not work, but changing the binding to:

    IsVisible="{Binding ShowShare, ElementName=root}"

    where root is the name of the UserControl element in the XAML works.
  26. Rob Ainscough
    Rob Ainscough avatar
    174 posts
    Member since:
    Jan 2010

    Posted 05 Mar 2015 Link to this post

    I'm using Q1 2015 UI Silverlight Controls and the IsVisible binding to MVVM issue still remains? 

    I've tried all the suggested work arounds viable for MVVM and none have worked.

    Looks like Telerik have just stopped responding to this thread (which is 4 years long) even though it appears to be a need for many developers (myself included).

    So is Telerik's official response "too difficult to implement"?

    Cheers, Rob.


  27. Rob Ainscough
    Rob Ainscough avatar
    174 posts
    Member since:
    Jan 2010

    Posted 05 Mar 2015 Link to this post

    UPDATE: I did try Maya sample project (bindingtoisvisible.zip) ... it does work BUT it's really only practical for demonstration purposes.  

    A production application would never instantiate the VM that way -- just not flexible ... I would expect (at least ours) production applications to setup the DataContext in a New instance of the UserControl passing whatever parameters are needed.

    I was able to setup a checkbox that binds to IsVisible on a column but again that's user interaction which is not MVVM where I need to setup the IsVisible on the columns based on some parameter/state.

    So at this point we've hit a rather significant show stopper unless this is implemented.  Being able to hide columns in a grid using MVVM is key for our requirements.

    Does anyone have any suggestions of alternate vendors that might provide such functionality in their Grids?

    Cheers, Rob.
  28. Rob Ainscough
    Rob Ainscough avatar
    174 posts
    Member since:
    Jan 2010

    Posted 06 Mar 2015 in reply to Rodrigo Rodriguez Link to this post

    FYI, this is the ONLY approach that has worked for me for MVVM.  Big thank you to Rodrigo.

    A lot of code for something so so so very simple ... it's one of those 14 years ago it would take 3 seconds and one line of code to hide a column ... today it's add a helper class, 20-30 lines of code ... this is progress??  If it is, then the future of software development is going to be pretty bleak. 

    Cheers, Rob.
Back to Top