Search code examples
c#-4.0wpfdatagridwpf-4.0

How to create Custom Property for WPF DataGridTextColumn


I want to add custom property in WPF DataGridTextColumn,

How i can add custom property, and Bind it from C# code and retrieve it by C# code.


Solution

  • I just got answer for it

    First Create

     public static class dataGridTag
        {
            public static readonly DependencyProperty TagProperty = DependencyProperty.RegisterAttached(
               "Tag",
               typeof(object),
               typeof(dataGridTag),
               new FrameworkPropertyMetadata(null));
    
            public static object GetTag(DependencyObject dependencyObject)
            {
                return dependencyObject.GetValue(TagProperty);
            }
    
            public static void SetTag(DependencyObject dependencyObject, object value)
            {
                dependencyObject.SetValue(TagProperty, value);
            } 
        }
    

    For Binding tag property by C#

             DataGridTextColumn clm = new DataGridTextColumn();
             dataGridTag.SetTag(clm, "TagValue");
    

    For Retrieving tag property by C#

             DataGridColumn clm1 = dgQuestionTemplate.CurrentCell.Column as DataGridColumn;
    
             string strQType=  dataGridTag.GetTag(clm1).ToString();