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

[Solved] Binding IsVisible with string indexer

1 Answer 176 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.
Andrea Rapuzzi
Top achievements
Rank 1
Andrea Rapuzzi asked on 03 Feb 2011, 11:48 AM
Hello,

I would like to bind the IsVisible property of a GridViewDataColumn to a value in a dictionary with string indexer.

This is what I've tried, but without success

The UserControl
<UserControl.Resources>
        <vm:HumanResourcesModelViewModule x:Key="ViewModel"/>
    </UserControl.Resources>
  
    <Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource ViewModel}">
        <telerik:RadGridView AutoGenerateColumns="False" HorizontalAlignment="Left" Name="radGridView1" VerticalAlignment="Top" ItemsSource="{Binding Users}" >
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="Name" IsVisible="{Binding [Name], Source={StaticResource ViewModel}}" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Surname}" Header="Surname" IsVisible="{Binding [Surname], Source={StaticResource ViewModel}}" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Phone}" Header="Phone" IsVisible="{Binding Visibilities[Phone], Source={StaticResource ViewModel}}" />
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
And the ViewModel
public class HumanResourcesModelViewModule
   {
       private ObservableCollection<User> users;
       public ObservableCollection<User> Users
       {
           get 
           {
               return users;
           }
           set
           {
               users = value;
           }
       }
       public HumanResourcesModelViewModule()
       {
           users = new ObservableCollection<User> ( );
           User u = new User ( );
           u.Name = "Test";
           u.Surname = "Prova";
           u.Phone = "0331973166";
           u.Visibilities.Add("Name",true);
           u.Visibilities.Add("Surname", true);
           u.Visibilities.Add("Phone", false);
           users.Add ( u );
       }
       public class User : INotifyPropertyChanged
       {
           private Dictionary<string, bool> visibilities = new Dictionary<string, bool> ( );
           public string Name { get; set; }
           public string Surname { get; set; }
           public string Phone { get; set; }
           public Dictionary<string, bool> Visibilities
           {
               get { return this.visibilities; }
               set
               {
                   if ( this.visibilities != value )
                   {
                       visibilities = value;
                       this.RaisePropertyChanged ( "Visibilities" );
                   }
               }
           }
           public bool this[string columnName]
           {
               get
               {
                   return visibilities[columnName];
               }
               set
               {
                   visibilities[columnName] = value;
                   this.RaisePropertyChanged ( columnName );
               }
           }
           protected virtual void RaisePropertyChanged ( string propertyName )
           {
               var handler = PropertyChanged;
               if ( handler != null )
               {
                   handler ( this,
                   new PropertyChangedEventArgs ( propertyName ) );
               }
           }
           protected virtual void RaisePropertyChanged ( object o, PropertyChangedEventArgs e )
           {
               var handler = PropertyChanged;
               if ( handler != null )
               {
                   handler ( o, e );
               }
           }
           public event PropertyChangedEventHandler PropertyChanged;
       }
   }

Thanks!

Andrea

1 Answer, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 03 Feb 2011, 12:22 PM
Hi,

 The data context in columns is the data context of the grid itself - not the data item. 

Best wishes,
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>>
Tags
GridView
Asked by
Andrea Rapuzzi
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Share this question
or