views:

93

answers:

1

Hi,

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 namespace anywhere...

Best Regards, Jesper

+2  A: 

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.

Ray Burns
As I'm sorting the TabItems in a TabControl I decided to use the KeyboardNavigation.TabIndex attached property, which seems to fit in with the concept and this will hopefully not give me any problems.
Krimson