Welcome to the Xceed Community | Help
Community Search  
More Search Options

DataGrid 3.7 Column Visible Binding Question

Sort Posts: Previous Next
  •  04-20-2010, 4:57 PM Post no. 26563

    DataGrid 3.7 Column Visible Binding Question

    Greetings,

    I am trying to bind the column visible to my viewmodel bool property and I am getting binding errors that I figured would work.  I get an error in the output window stating..

    System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Xceed.Wpf.DataGrid.DataGridControl', AncestorLevel='1''. BindingExpression:Path=DataContext.IsNameVisible; DataItem=null; target element is 'Column' (HashCode=41921117); target property is 'Visible' (type 'Boolean')

    Here is my snippet

    <xcdg:DataGridControl SelectionMode="Single" AutoCreateColumns="False"

    ItemsSource="{Binding Source={StaticResource SearchResults}}">

     

    <xcdg:DataGridControl.Columns>

    <xcdg:Column FieldName="Name" Title="Name" Visible="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type xcdg:DataGridControl},}, Path=DataContext.IsNameVisible, Mode=TwoWay}" />

    .. other columns removed for ease.

    </xcdg:DataGridControl.Columns>

    <xcdg:DataGridControl.View>

    <xcdg:TableView AllowColumnChooser="False">

    <xcdg:TableView.Theme>

    <xcdg:Office2007BlueTheme />

    </xcdg:TableView.Theme>

    </xcdg:TableView>

    </xcdg:DataGridControl.View>

    </xcdg:DataGridControl>

    I want do have a couple of canned views in the viewmodel that a user can choose via a drop down list that would automatically hide/show columns all at once and not have the user use the ColumnChooser.

    I was also thinking if I could gain access to the ColumnChooser in code, maybe I could loop through the collection and update the columnchooser bool that way as well.  is that possible?

    Thanx

  •  09-23-2010, 10:13 AM Post no. 28791 in reply to 26563

    Re: DataGrid 3.7 Column Visible Binding Question

    Column is not part of the VisualTree. Therefore, FindAncestor will not work; neither will a DataContext Binding as the DataContext is not propagated to the non-visual elements.

    For any element that does not have a DataContext, or that cannot find a context, the following article explains various techniques. In this scenario, DataContextSpy might be a good option.
    http://www.codeproject.com/KB/WPF/ArtificialInheritanceCxt.aspx

    Otherwise, in version 4.1 (planned for late October), ColumnBase will have a DataContext property that will return the DataContext of the ParentDataGridControl.

     


    ** Quick Tip: Clients with an active support subscription should be sending their questions by email if they wish to benefit from the faster response time. Thanks!


    Diane Lafontaine
    Technical Support
    Xceed Software Inc.
  •  05-17-2012, 3:22 PM Post no. 32118 in reply to 28791

    Re: DataGrid 3.7 Column Visible Binding Question

    I don't see the DataContext property in the 4.3 documentation of ColumnBase. Was this delayed?
  •  07-05-2012, 3:16 PM Post no. 32352 in reply to 32118

    Re: DataGrid 3.7 Column Visible Binding Question

    Hi Bryan,

    It turns out this was added differently than the information I had at the time of my previous post. Instead we added a DataGridControl property that makes it possible to reach the DataContext.

    Reference:
    http://xceed.com/CS/forums/thread/29211.aspx

     


    ** Quick Tip: Clients with an active support subscription should be sending their questions by email if they wish to benefit from the faster response time. Thanks!


    Diane Lafontaine
    Technical Support
    Xceed Software Inc.
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.