1

I have a reference to a GridViewColumn in my xaml code as RuntimeColumn but I am not able to disable it or set it to readonly programmatically. I will need to do this at runtime without databinding.

I tried:

this.RuntimeColumn.IsEnabled = false;
this.RuntimeColumn.ReadOnly = false;

Any ideas?

Joan Venge
  • 315,713
  • 212
  • 479
  • 689

1 Answers1

2

You will have to set an EventSetter with Loaded Event, and in your code behind put the following

private void GridViewColumnHeader_Loaded(object sender, RoutedEventArgs e)
    {            
        GridViewColumnHeader columnHeader = sender as GridViewColumnHeader;
        Border HeaderBorder = columnHeader.Template.FindName("HeaderBorder", columnHeader) as Border;
        if (HeaderBorder != null)
        {
            HeaderBorder.Background = HeaderBorder.Background;
        }
        Border HeaderHoverBorder = columnHeader.Template.FindName("HeaderHoverBorder", columnHeader) as Border;
        if (HeaderHoverBorder != null)
        {
            HeaderHoverBorder.BorderBrush = HeaderHoverBorder.BorderBrush;
        }
        Rectangle UpperHighlight = columnHeader.Template.FindName("UpperHighlight", columnHeader) as Rectangle;
        if (UpperHighlight != null)
        {
            UpperHighlight.Visibility = UpperHighlight.Visibility;
        }
        Thumb PART_HeaderGripper = columnHeader.Template.FindName("PART_HeaderGripper", columnHeader) as Thumb;            
        if (PART_HeaderGripper != null)
        {
            PART_HeaderGripper.Background = PART_HeaderGripper.Background;
            PART_HeaderGripper.Cursor = System.Windows.Input.Cursors.Arrow; // override the size curser
        }
    }
Pacman
  • 2,183
  • 6
  • 39
  • 70
  • Thanks, this code is very new to me. Which one retrieves a particular GridColumnView by name, say "RuntimeColumn", set as x:Name="RuntimeColumn"? – Joan Venge Mar 16 '11 at 17:19
  • in the xaml, set the HeaderContainerStyle to a style, and set the – Pacman Mar 16 '11 at 19:06
  • 1
    @Joan Venge, only the column that you want disabled, needs to be styled with the "Loaded" event. – Pacman Mar 20 '11 at 13:46