如何使GridViewColumn只读/禁用?

时间:2011-03-16 00:26:47

标签: c# .net wpf listview gridviewcolumn

我在我的xaml代码中引用GridViewColumnRuntimeColumn,但我无法将其禁用或以编程方式将其设置为只读。我需要在运行时不进行数据绑定。

我试过了:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您必须使用Loaded Event设置EventSetter,并在后面的代码中添加以下内容

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
        }
    }