Hide a Column Using Data Annotations in WPF -


i have grid in wpf, auto-generating columns. how can dynamically hide columns using data annotations?

i thought of having property in model specify whether column visible, i'm not sure how it.

my model, bound grid:

public class template {         public string county { get; set; }          public string operator { get; set; }          public string field { get; set; } } 

here sample uses attributes hide columns. uses attached property handle autogeneratingcolumn event.

hidecolumnifautogenerated.cs - attribute

namespace autohidecolumn {     public class hidecolumnifautogenerated : system.attribute     {         public hidecolumnifautogenerated()         {         }     } } 

datagridextension.cs - attached property

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 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 propertydescriptor;             if (propertydescriptor != null)             {                 foreach (var item in propertydescriptor.attributes)                 {                     if (item.gettype() == typeof(hidecolumnifautogenerated))                     {                         e.cancel = true;                     }                 }             }         }     } } 

xaml

<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">     <grid>         <grid.rowdefinitions>             <rowdefinition height="*"/>             <rowdefinition height="*"/>         </grid.rowdefinitions>             <datagrid name="dg" local:datagridextension.hideannotatedcolumns="true">          </datagrid>         <datagrid grid.row="1" name="dg1">          </datagrid>     </grid> </window> 

codebehind

using system.collections.generic; using system.windows;  namespace autohidecolumn {     /// <summary>     /// interaction logic mainwindow.xaml     /// </summary>     public partial class mainwindow : window     {         public mainwindow()         {             initializecomponent();              this.dg.itemssource = new list<customer>();             this.dg1.itemssource = new list<customer>();         }     }      public class customer     {         [hidecolumnifautogenerated()]         public int id { get; set; }          public string name { get; set; }     } } 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -