5

在 Xaml 中,我可以使用 local:TestClass.TestProperty="1" 设置自定义附加属性

我可以使用 {Binding Path=(Namespace:[OwnerType].[PropertyName])} {Binding Path=(local:TestClass.TestProperty)} 绑定到自定义附加属性

但是,当我需要在 SortDescription 中使用自定义附加属性时,如何指定命名空间?我可以使用 new SortDescription("(Grid.Row)", ListSortDirection.Descending) 绑定到附加属性,但在这里我不能在任何地方设置命名空间......

最好的问候, 杰斯珀

4

1 回答 1

2

您不能,原因与{Binding Path=a:b.c}有效但{Binding a:b.c}无效的原因相同:PropertyPath 构造函数没有命名空间上下文。

不幸的是,对于 SortDescription,您无能为力。您必须找到一种不使用附加属性的方式进行排序。

通常我会告诉人们使用 Tag 是错误编码的一个指标,但在这种情况下 Tag 可能是您最好的选择:您可以在 Tag 中创建一个对象,该对象具有返回您想要的实际附加属性的属性。

在您的 PropertyChangedCallback 中,将 Tag 实例化为内部类的实例:

public class TestClass : DependencyObject
{
  ... TestProperty declaration ...
  PropertyChangedCallback = (obj, e) =>
  {
    ...
    if(obj.Tag==null) obj.Tag = new PropertyProxy { Container = obj };
  });

  public class PropertyProxy
  {
    DependencyObject Container;
    public SomeType TestProperty { get { return GetTestProperty(Container); } }
  }
}

现在您可以在 SortDescription 中使用 Tag 的子属性:

<SortDescription PropertyName="Tag.TestProperty" />

如果只有一个属性需要排序,您可以简单地使用标签。

这样做的主要问题是使用 Tag 属性将与任何其他也尝试使用 Tag 的代码冲突。因此,您可能想在标准库中寻找一些甚至不适用于相关对象的晦涩的 DependencyProperty,并使用它来代替 Tag。

于 2010-06-16T19:39:26.970 回答