开发者

SortDescription with custom attached property

开发者 https://www.devze.com 2023-01-03 21:02 出处:网络
In Xaml I can set a custom attached property using local:TestClass.TestProperty=\"1\" An I can bind to a custom attached property using

In Xaml I can set a custom attached property using local:TestClass.TestProperty="1"

An I can bind to a custom attached property using {Binding Path=(Namespace:[OwnerType].[PropertyName])} {Binding Path=(local:TestClass.TestProperty)}

But how do I specify the namespace when I need to use a custom attached property in a SortDescription? I can bind to an attached property using new SortDescription("(Grid.Row)", ListSortDirection.Descending) but here I can't set a name开发者_JS百科space anywhere...

Best Regards, Jesper


You can't, for the same reason that {Binding Path=a:b.c} works but {Binding a:b.c} doesn't: The PropertyPath constructor has no namespace context.

Unfortunately in the case of SortDescription there isn't much you can do about it. You have to find a way to sort without using attached properties.

Normally I tell people that use of Tag is an indicator of bad coding, but in this case Tag may be your best option: You can create an object within Tag that has properties that return the actual attached properties you want.

In your PropertyChangedCallback, instantiate Tag to an instance of an inner class:

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

Now you can use the sub-property of Tag in your SortDescription:

<SortDescription PropertyName="Tag.TestProperty" />

If there is only a single property to be sorted, you can simply use the Tag for it.

The main problem with this is that using the Tag property will conflict with any other code that also tries to use the Tag. So you may want to look for some obscure DependencyProperty in the standard libraries that doesn't even apply to the objects in question and use that instead of Tag.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号