您不能,原因与{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。