1

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

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

4

1 回答 1

3

我刚刚得到了答案

首先创建

 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();
于 2013-07-31T12:30:10.513 回答