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

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

问题描述

我需要在与标题不同的 Datagrid 列中保存一个字符串.

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

这是必需的,因为我动态生成一个 Datagrid 并希望在生成它们时转换列标题.然后我将整个 XAML 绑定到一个 ContentControl.

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.

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

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.

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

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

推荐答案

在 WPF 中,通过使用 附加属性.可以在任何 DependencyObject 上设置附加属性.这种附加属性的一个很好的例子是 Grid.Row.由于您可以定义它们,因此您也可以将它们命名为比 Tag 更有意义的名称.

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

}

用法:

<DataGridColumn SomeClass.Tag="abc" />

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

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