Get binding Path of GridViewColumn RRS feed

  • Question

  • I'm trying to get the binding Path of a GridViewColumn that has a CellTemplate set on it. I've found an article that says just use

    string dataColumn = (gv.Columns[i].DisplayMemberBinding as Binding).Path.Path;

    but it doesn't seem to work for me (is always null). My app is .Net 3.5 with VS2008.
    Thursday, November 6, 2008 5:14 PM


  • Hi,

    Here is a small example of getting the binding path setting in GridViewColumn in ListView.


    In the XAML code,  I just create a ListView and two Button. In terms of the GridView in that ListView, for comparison purpose, we set one GridViewColumn use DisplayMemberBinding property directly, and another use CellTemplate with a DataTemplate.


    In the code behind,  if the GridViewColumn use CellTemplate, First, we get a ListViewItem instance , then get the TextBlock that in the DataTempalte from the VisualTree by name, Finally, just invoke the BindingOperations’s GetBinding method to get the binding instance  and get its path property.


    XAML code:


            <ListView Name="listview">

                <ListView.View >


                        <GridViewColumn Header="HYear">



                                    <TextBlock Name="textYear" Text="{Binding Path=Year}" />




                        <GridViewColumn Header="textDayOfWeek" DisplayMemberBinding="{Binding DayOfWeek}" />



                <s:DateTime >1/1/2007</s:DateTime>

                <s:DateTime >1/2/2007</s:DateTime>

                <s:DateTime >1/3/2007</s:DateTime>



            <Button Width="200" Height="30" Content="get Binding's Path not in CellTemplate" Click="getBindingNotInTemplate" />

            <Button Width="200" Height="30" Content="get Binding's Path within CellTemplate" Click="getBindingInTemplate" />



    In the code behind:


    public partial class Window1 : Window


            public Window1()






            //get the path that binding not in CellTemplate

            private void getBindingNotInTemplate(object sender, RoutedEventArgs e)


                //string dataColumn = (gv.Columns[i].DisplayMemberBinding as Binding).Path.Path;

                GridView gridview = listview.View as GridView;

                string datacolumn = (gridview.Columns[1].DisplayMemberBinding as Binding).Path.Path;

                MessageBox.Show("Binding path is:"+datacolumn );



            //simply set the selected item's index to 0

            int index = 0;

            //get the path that binding within CellTemplate

            private void getBindingInTemplate(object sender, RoutedEventArgs e)


                ListViewItem item = listview.ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem;

                TextBlock text = GetvisualChildByName(item, "textYear") as TextBlock;

                Binding bind = BindingOperations.GetBinding(text, TextBlock.TextProperty);

                string dataColumn2 = bind.Path.Path;

                MessageBox.Show("Binding path is:" + dataColumn2);


            //get the element is the VisualTree

            public static Visual GetvisualChildByName(Visual element, string name)



                if (element == null) return null;

                if (element is FrameworkElement && (element as FrameworkElement).Name == name) return element;


                Visual result = null;

                if (element is FrameworkElement)


                    (element as FrameworkElement).ApplyTemplate();



                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)


                    Visual visual = VisualTreeHelper.GetChild(element, i) as Visual;

                    result = GetvisualChildByName(visual, name);

                    if (result != null)



                return result;




    If you have any other questions with this, just feel free to ask.



    Jim Zhou -MSFT
    • Proposed as answer by Marco Ragogna Tuesday, November 11, 2008 9:29 AM
    • Marked as answer by SRichardson Tuesday, November 11, 2008 12:45 PM
    Tuesday, November 11, 2008 9:08 AM