question

EmonHaque-1485 avatar image
0 Votes"
EmonHaque-1485 asked EmonHaque-1485 edited

How to invoke DependencyProperty's PropertyChangedCallback after ICollectionView's filter?

Here in my Receipt and Payments view, I've the EditText and HiBlock to Search, Filter and Highlight content of ICollectionView.

99900-test.gif

the Text property of EditText is bound to view model's Query property:

     string query;
     public string Query {
         get { return query; }
         set {
             if (query != value) {
                 query = value;
                 Debug.WriteLine("In VM");
                 RPs.Refresh();
             }
         }
     }

where I refresh the ICollectionView and HiBlock's Query property where I've the logic for highlighting. When I typed a after mr in the EditText, it first printed In VM in the Output window and started printing In HiBlock ... immediately. There wasn't that much entries in the ListBox BUT, looks like, the Query PropertyChangedCallback of HiBlock was invoked for all visible as well as invisible lines (HiBlock) in the ListBox and even though I didn't see/have the invisible HiBlocks in the ListBox, the Visibility property of HiBlock was Visible in the PropertyChanged callback, probably, because it called onQueryChanged in HiBlock and RPs.Refresh() in view model parallelly. Right now, it's very slow.

What I want it do is finish the RPs.Refresh() in the view model first in order to make sure that the Visibility of invisible HiBlocks becomes Collapsed and then perform the highlighting logic only on the Visible HiBlocks.

How to do that?

EDIT


On thing's come to mind is create an extra property in the view model, reset that after refresh and notify, and finally have a relative source binding with HiBlock's Query on that property in the DataTemplate BUT that's a lot of work! Is there any better way?

EDIT


BUT what came to mind, didn't work as I expected:

tried to add another GIF (2.19MB) here BUT couldn't!

When I'd mr in the EditText, there're only 35 Visible HiBlocks in the ListBox, 2 of them Receipt and Payment are not queryable, and when I typed a it'd gotten in to Query PropertyChangeCallback 196 times altogether! In the view model, I've changed newly added HilightQuery after Refresh:

 string query;
 public string Query {
     get { return query; }
     set {
         if (query != value) {
             query = value;
             Debug.WriteLine("In VM");
             RPs.Refresh();
             HilightQuery = query;
         }
     }
 }
 string hilightQuery;
 public string HilightQuery {
     get { return hilightQuery; }
     set {
         if (hilightQuery != value) {
             hilightQuery = value;
             OnPropertyChanged(nameof(HilightQuery));
         }
     }
 }

and in the Group ControlTemplate instead of setting Query property directly from EditText, I've this binding

  header.SetBinding(HiBlock.TextProperty, new Binding(nameof(GroupItem.Name)));
 //header.SetBinding(HiBlock.QueryProperty, new Binding(nameof(EditText.Text)) { Source = querySource, IsAsync = true });
 header.SetBinding(HiBlock.QueryProperty, new Binding() {
     Path = new PropertyPath("DataContext.HilightQuery"),
     RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(ListBox), 1)
 });

and in ItemTemplate this:

 tenantName.SetBinding(HiBlock.TextProperty, new Binding(nameof(ReceiptPayment.Tenant)));
 //tenantName.SetBinding(HiBlock.QueryProperty, new Binding(nameof(EditText.Text)) { Source = querySource, IsAsync = true });
 tenantName.SetBinding(HiBlock.QueryProperty, new Binding() {
     Path = new PropertyPath("DataContext.HilightQuery"),
     RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(ListBox), 1)
 });

why does it say HiBlock is Visible and get into the PropertyChangedCallback so many times even though they're clearly Invisible/Collapsed?

windows-wpf
test.gif (1.1 MiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

EmonHaque-1485 avatar image
0 Votes"
EmonHaque-1485 answered EmonHaque-1485 edited

The problem here is the DependencyProperty or INotifyPropertyChanged. When I bind the HiBlock's Query property to the DepenDencyProperty of EditText's Text and change that Text property, it fires PropertyChanged notification BUT looks like filterable ListBox, that reevaluates ListBoxItems doesn't require any PropertyChanged notification. Now, instead of binding EditText's Text, I've bound viewmodel's Query property.

And if I call OnPropertyChanged here in the setter of viewmodel's Query property, I've same problem. It calls that PropertyChangedCallback at least over 100 times and few times I've seen it's called over 300 times. When the filter is applied, it actually calls HiBlock's PropertyChangedCallback automatically and also looks at the content of viewmodel's Query property to do what it's supposed to do:

100103-test.gif

Very interesting, got the UI updated without notification!


test.gif (2.6 MiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.