WPF

[WPF] Delay 이후 UI 작업을 진행할 때

Chanhongv 2024. 9. 11. 13:38

1. ItemsSource 와 CollectionViewSource 를 업데이트 하고

2. Filter  를 적용하여 UI Update 

 

를 한 함수에서 실행하게끔 의도를 하였는데 Binding Error 가 발생하는 것을 목격했다.

에러 내용

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'ListBoxItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')

 

그래서 1번 실행 후 딜레이를 주고 2번을 실행하게 했더니 Binding Error 가 발생하지 않았다.

 

시도한 방법은 두가지이다.

 

A. 실행 함수를 비동기 함수로 변경

 - 1번 실행

 - await Task.Delay(10);

 - 2번 실행

 

B. 동기 함수에서

 - 1번 실행

Task.Delay(10).ContinueWith(_ =>
{
    Dispatcher.Invoke(() =>
    {
        OnSearchInputItems(SearchText); // 2번 함수
    });
});

 

 

위 구문을 사용하면 Task.Delay(10) 동안에 UI Thread 를 사용하는 것이 아니라서 UI 의 멈춤을 유발하지 않고 10ms 기다린 후 ContinueWith 다음 내가 의도한 함수를 Dispatcher.Invoke(() => 에 넣어서 실행하면 된다.