如何创建WPF DataGridTextColumn自定义属性(How to create Cust

2019-10-18 11:06发布

我想在WPF DataGridTextColumn添加自定义属性,

如何我可以添加自定义属性,并从C#代码绑定它和C#代码检索。

Answer 1:

我刚刚得到的答案吧

首先创建

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

对于由C#绑定标签属性

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

对于由C#检索标记属性

         DataGridColumn clm1 = dgQuestionTemplate.CurrentCell.Column as DataGridColumn;

         string strQType=  dataGridTag.GetTag(clm1).ToString();


文章来源: How to create Custom Property for WPF DataGridTextColumn