Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a sample which uses attributes to hide columns. It uses an attached property to handle the <code>AutoGeneratingColumn</code> event.</p> <p><strong>HideColumnIfAutoGenerated.cs</strong> - Attribute</p> <pre><code>namespace AutoHideColumn { public class HideColumnIfAutoGenerated : System.Attribute { public HideColumnIfAutoGenerated() { } } } </code></pre> <p><strong>DataGridExtension.cs</strong> - Attached Property</p> <pre><code>using System.ComponentModel; using System.Windows; using System.Windows.Controls; namespace AutoHideColumn { public static class DataGridExtension { public static readonly DependencyProperty HideAnnotatedColumnsProperty = DependencyProperty.RegisterAttached( "HideAnnotatedColumns", typeof(bool), typeof(DataGridExtension), new UIPropertyMetadata(false, OnHideAnnotatedColumns)); public static bool GetHideAnnotatedColumns(DependencyObject d) { return (bool)d.GetValue(HideAnnotatedColumnsProperty); } public static void SetHideAnnotatedColumns(DependencyObject d, bool value) { d.SetValue(HideAnnotatedColumnsProperty, value); } private static void OnHideAnnotatedColumns(DependencyObject d, DependencyPropertyChangedEventArgs e) { bool hideAnnotatedColumns = (bool)e.NewValue; DataGrid dataGrid = d as DataGrid; if (hideAnnotatedColumns) { dataGrid.AutoGeneratingColumn += dataGrid_AutoGeneratingColumn; } else { dataGrid.AutoGeneratingColumn -= dataGrid_AutoGeneratingColumn; } } private static void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { PropertyDescriptor propertyDescriptor = e.PropertyDescriptor as PropertyDescriptor; if (propertyDescriptor != null) { foreach (var item in propertyDescriptor.Attributes) { if (item.GetType() == typeof(HideColumnIfAutoGenerated)) { e.Cancel = true; } } } } } } </code></pre> <p><strong>XAML</strong></p> <pre><code>&lt;Window x:Class="AutoHideColumn.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:AutoHideColumn" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="*"/&gt; &lt;RowDefinition Height="*"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;DataGrid Name="dg" local:DataGridExtension.HideAnnotatedColumns="True"&gt; &lt;/DataGrid&gt; &lt;DataGrid Grid.Row="1" Name="dg1"&gt; &lt;/DataGrid&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p><strong>CodeBehind</strong></p> <pre><code>using System.Collections.Generic; using System.Windows; namespace AutoHideColumn { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.dg.ItemsSource = new List&lt;Customer&gt;(); this.dg1.ItemsSource = new List&lt;Customer&gt;(); } } public class Customer { [HideColumnIfAutoGenerated()] public int ID { get; set; } public string Name { get; set; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload