SortDescription with custom property attached

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

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

But how do I specify a namespace when I need to use a custom attached property in SortDescription? I can bind to the attached property using the new SortDescription ("(Grid.Row)", ListSortDirection.Descending) but here I cannot set the namespace anywhere ...

Regards, Jesper

+3
source share
1 answer

You cannot for the same reason that it {Binding Path=a:b.c}works, but it {Binding a:b.c}does not work: the PropertyPath constructor does not have a namespace context.

Unfortunately, with SortDescription, there is little you can do. You should find a way to sort without using attached properties.

I usually tell people that using a tag is an indicator of bad coding, but in this case, the tag may be your best option: you can create an object in the tag that has properties that return the properties you want.

In your PropertyChangedCallback, create an instance tag of the 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 Tag sub-property in your SortDescription:

<SortDescription PropertyName="Tag.TestProperty" />

If there is only one property to sort, you can simply use the tag for it.

, Tag , . , - DependencyProperty , , .

+2

Source: https://habr.com/ru/post/1750298/


All Articles