This is a migrated thread and some comments may be shown as answers.

[Solved] Binding column header property in bound grid

4 Answers 240 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Anne Lyon
Top achievements
Rank 1
Anne Lyon asked on 17 Jan 2011, 12:04 PM

HI,
I have a grind which is bound to an object list to display all the rows:
<telerik1:RadGridView x:Name="GridParamAttr" ItemsSource="{Binding AttribList}"

Now I would really like to bind the IsVisible attribute of some the columns to an boolean item “IsUserAllowed”, meaning that if the user for  this particular rowselection is allowed to see all columns they will all be visible, otherwise some columns will be hidden.

But as the GridView is already bound to the AttribList (contains all rows) I only seem to be able to make the content hidden for the particular column I want to hide. If I bind “IsUserAllowed” to the IsVisible attribute for the GridViewColumn it is just ignored.

 

How can I bind the “IsVisible” property for a GridViewColumn so that my column will automatically be visible or hidden depending on the true/false value of my “IsUserAllowed” attribute (fetched from a DB) - in addition to being bound to a datasource to fill in all rows?

 

Thanks,
ANne

4 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 17 Jan 2011, 01:01 PM
Hi,

 The grid header row DataContext is the same as the DataContext for the grid itself. You should verify if you have such property (IsUserAllowed) in this DataContext. ItemsSource items are used as DataContext of the grid data rows. 

Regards,
Vlad
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Anne Lyon
Top achievements
Rank 1
answered on 17 Jan 2011, 03:04 PM
Yes, that is how simple I imagined it would be! My code in XAML is the following where I bind the column's isVisible attribute to my QAVisibilityBool property in my bound list. For test purposes I have a similar attribute for an image in each cell, bound to a property of type Visibility. When my flag "IsUserAllowed" is false, then QAVisibilityBool is set to false, and QAVisibility is set to Collapsed. The images in all cells in the column disappear, but the column is still visible.

<telerik1:GridViewColumn Header="Do QA" IsVisible="{Binding QAVisibilityBool}">
      <telerik1:GridViewColumn.CellTemplate>
         <DataTemplate>
             <Image  Visibility="{Binding QAVisibility}"  Source="Resources/Pencil.png" Stretch="None"  />
         </DataTemplate>
     </telerik1:GridViewColumn.CellTemplate>

Here is the code in the bound object:
public Visibility QAVisibility
   {
       get
       {
           return myQAVisibility;
       }
       set
       {
           myQAVisibility = value;
           OnNotifyPropertyChanged("QAVisibility");
       }
   }
public Boolean QAVisibilityBool
{
    get
    {
        return myQAVisibilityBool;
    }
    set
    {
        myQAVisibilityBool = value;
        OnNotifyPropertyChanged("QAVisibilityBool");
    }
}

The get for QAVisibility is called for each row, the get for QAVisibilityBool is never called (but is set for each row so PropertyChanged is called!). What am I doing wrong here?  :-)
0
Vlad
Telerik team
answered on 17 Jan 2011, 03:08 PM
Hi,

This property cannot be accessed from column properties binding since it is part of the data row DataContext (please check my previous reply). The columns have the same DataContext as the grid. 

Greetings,
Vlad
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Anne Lyon
Top achievements
Rank 1
answered on 18 Jan 2011, 10:32 AM

Vlad, I am sorry I don't seem to understand what my problem is and what your answers mean...
The ItemSource for my grid is "AttribList". (see definition of grid on my first post) This contains all the rows for my grid with attributes bound to GridDataViewColumns, example:  
DataMemberBinding="{Binding QAUser}".
AttribList also contains an attribute called "QAVisibilityBool" This I bind to one particular GridViewColumn: <telerik1:GridViewColumn Header="Do QA" IsVisible="{Binding QAVisibilityBool}">. This should control if the column should be visible or not, but it does not work.

I understand that (ref. your first reply) “ItemsSource items are used as DataContext of the grid data rows.” And that works fine for the rows. But how do I set properties for the grid header row?  When you say: “The grid header row DataContext is the same as the DataContext for the grid itself.”, does that mean that the DataContext for the grid header row is set or NOT set when I declare ItemSource=”{Binding AttribList}”?

In your second answer you say that “This property cannot be accessed from column properties binding since it is part of the data row DataContext”. So I cannot access QAVisibilityBool since it belongs to AttribList which is ItemSource for the grid contents? But then you say “The columns have the same DataContext as the grid.” I understand that reply as the grid header will use attributes from the same DataContext as the grid contents?? So I am confused…

My main Layout Grid has set a DataContext="{Binding Source={StaticResource AttribVM}}" where AttribVM is a class containing all data for my page. AttribList is a ObservableCollection member. RadGridView is a child of the Layout Grid. I have tried to change AttribVM to also contain a member “columnVisability” and bind the isVisible property of my particular GridViewColumn to this member, but still no result.

Do you have any examples that show how you bind datasources to a grid in such a way that you can control both Column headers and contents from your bound object?

 

Thanks,

Tags
GridView
Asked by
Anne Lyon
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Anne Lyon
Top achievements
Rank 1
Share this question
or