WPF DataGrid列中的标签属性 [英] Tag Property in WPF DataGrid Column

查看:165
本文介绍了WPF DataGrid列中的标签属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是需要的,因为我动态地生成了一个Datagrid,并且想要转换Column Headers同时生成它们。然后我将整个XAML绑定到ContentControl。



直到这里没有问题...但是我想重新排序和调整列的大小,所以我需要查找afterwoods。为此,我需要原始(未翻译)ColumnHeader。



在我看来,列的Tag属性可以解决这个问题,但是没有:(

解决方案

在WPF中,使用附加属性可以在任何 DependencyObject 上设置附加属性。这样附加的属性是 Grid.Row 。由于您可以定义它们,因此您还可以将其命名为比Tag更有意义的内容。



定义附加属性的示例代码:

  public static class SomeClass {

public static readonly DependencyProperty TagProperty = DependencyProperty.RegisterAttached(
Tag,
typeof(object),
typeof(SomeClass),
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);
}

}

用法:

 < DataGridColumn SomeClass.Tag =abc/> 


I need to save an string inside a Datagrid Column which differs from the Header.

This is needed because I generate a Datagrid dynamically and want to translate the Column Headers while generating them. Then I bind the whole XAML to a ContentControl.

No problem till here... But I want to reorder and resize the columns, so I need to lookup them afterwoods. For this I need the original (not translated) ColumnHeader.

In my opinion a Tag property of the column would solve this problem, but there is no :(

解决方案

In WPF, you have virtually unlimited "Tag" properties by using Attached Properties. An attached property can be set on any DependencyObject. A good example of such an attached property is Grid.Row. Since you can define them, you also have the possibility of naming them something more meaningful than Tag.

Sample code for defining an attached property:

public static class SomeClass {

    public static readonly DependencyProperty TagProperty = DependencyProperty.RegisterAttached(
        "Tag",
        typeof(object),
        typeof(SomeClass),
        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);
    }

}

Usage :

<DataGridColumn SomeClass.Tag="abc" />

这篇关于WPF DataGrid列中的标签属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆