代码之家  ›  专栏  ›  技术社区  ›  Rachel

当autogenerateColumns=true时,是否有方法隐藏DataGrid中的特定列?

  •  23
  • Rachel  · 技术社区  · 14 年前

    我有一个WPF 4.0数据报,它使用autoGenerateColumns=true绑定到数据表。列是动态的,但是我知道总会有一个名为id的列,我想隐藏这个列。我有办法做到这一点吗?

    5 回复  |  直到 6 年前
        1
  •  34
  •   akjoshi HCP    10 年前

    AutoGeneratingColumn args DataGridAutoGeneratingColumnEventArgs Cancel Cancel = true

        2
  •  9
  •   Eric Ouellet    6 年前

    <Window
    ...
     xmlns:extension="clr-namespace:WpfControlLibrary.Extension;assembly=WpfControlLibrary">
    
    <DataGrid ...
        extension:DataGridBehavior.UseBrowsableAttributeOnColumn="True">
    

    public class YourObjectItem
    {
        [Browsable(false)]
            public Assembly Assembly { get; set; }
    

    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace HQ.Wpf.Util.Behaviors
    {
        /// <summary>
        /// Using this behavior on a dataGRid will ensure to display only columns with "Browsable Attributes"
        /// </summary>
        public static class DataGridBehavior
        {
            public static readonly DependencyProperty UseBrowsableAttributeOnColumnProperty =
                DependencyProperty.RegisterAttached("UseBrowsableAttributeOnColumn",
                typeof(bool),
                typeof(DataGridBehavior),
                new UIPropertyMetadata(false, UseBrowsableAttributeOnColumnChanged));
    
            public static bool GetUseBrowsableAttributeOnColumn(DependencyObject obj)
            {
                return (bool)obj.GetValue(UseBrowsableAttributeOnColumnProperty);
            }
    
            public static void SetUseBrowsableAttributeOnColumn(DependencyObject obj, bool val)
            {
                obj.SetValue(UseBrowsableAttributeOnColumnProperty, val);
            }
    
            private static void UseBrowsableAttributeOnColumnChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
            {
                var dataGrid = obj as DataGrid;
                if (dataGrid != null)
                {
                    if ((bool)e.NewValue)
                    {
                        dataGrid.AutoGeneratingColumn += DataGridOnAutoGeneratingColumn;
                    }
                    else
                    {
                        dataGrid.AutoGeneratingColumn -= DataGridOnAutoGeneratingColumn;
                    }
                }
            }
    
            private static void DataGridOnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
            {
                var propDesc = e.PropertyDescriptor as PropertyDescriptor;
    
                if (propDesc != null)
                {
                    foreach (Attribute att in propDesc.Attributes)
                    {
                        var browsableAttribute = att as BrowsableAttribute;
                        if (browsableAttribute != null)
                        {
                            if (!browsableAttribute.Browsable)
                            {
                                e.Cancel = true;
                            }
                        }
    
                        // As proposed by "dba" stackoverflow user on webpage: 
                        // https://stackoverflow.com/questions/4000132/is-there-a-way-to-hide-a-specific-column-in-a-datagrid-when-autogeneratecolumns
                        // I added few next lines:
                        var displayName = att as DisplayNameAttribute;
                        if (displayName != null)
                        {
                            e.Column.Header = displayName.DisplayName;
                        }
                    }
                }
            }
        }
    }
    
        3
  •  4
  •   Boern    10 年前

    Visibility.Collapsed

        private void dataGrid_AutoGeneratingColumn(object sender, 
            DataGridAutoGeneratingColumnEventArgs e)
        {
            //Set properties on the columns during auto-generation 
            switch (e.Column.Header.ToString())
            {
                case "rownameYouWantToHide":
                    e.Column.Visibility = Visibility.Collapsed;
                    break;
            }
        }
    
        4
  •  1
  •   Aaron McIver    14 年前

    AutoGenerateColumns=False

                <dg:DataGridTextColumn Header="Display" Binding="{Binding DisplayName}"/>
                <dg:DataGridTextColumn Header="Host" Binding="{Binding HostName}"/>
                <dg:DataGridTextColumn Header="Database" Binding="{Binding Database}"/>
                <dg:DataGridTextColumn Header="Username" Binding="{Binding Username}"/>
                <dg:DataGridTextColumn Header="Password" Binding="{Binding Password}"/>
    

    Header Property

        5
  •  0
  •   Kapitán Mlíko    11 年前

    public class Foo
    {
        public string Id { get; set; }
    
        public string Property2 { get; set; }
    
        public string Property3 { set; get; }
    }
    

    public class Foo2
    {
        public string Property2 { get; set; }
    
        public string Property3 { set; get; }
    }
    

    public class Foo
    {
        internal string Id { get; set; }
    
        public string Property2 { get; set; }
    
        public string Property3 { set; get; }
    }