정렬 UI 변경되지 않

0

질문

정렬

  <UserControl x:Class="NolowaFrontend.Views.MainViews.SearchView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:NolowaFrontend.Views.MainViews"
             xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
             xmlns:designTime="clr-namespace:NolowaFrontend.Views"
             mc:Ignorable="d" Name="_this"
             d:DesignHeight="300" d:DesignWidth="350" d:Background="White">   
    <Grid>
        <TextBlock Text="{Binding SearchedUsers.Count, diag:PresentationTraceSources.TraceLevel=High}" Foreground="Red" FontSize="20"/>
    </Grid>
</UserControl>

코드 숨김

public partial class SearchView : UserControl
{
    private readonly SearchVM _searchVM;

    public SearchView(User user)
    {
        InitializeComponent();

        _searchVM = new SearchVM(user); ;

        DataContext = _searchVM;
    }

    public void TimerSearch(string text)
    {
        _searchVM.TimerSearch(text);
    }
}

뷰 모델

public class SearchVM : ViewModelBase
{
    private readonly User _user;
    private readonly ISearchService _searchService;

    private ObservableCollection<SearchedUser> _searchedUsers = new ObservableCollection<SearchedUser>();

    public ObservableCollection<SearchedUser> SearchedUsers
    {
        get { return _searchedUsers; }
        set { _searchedUsers = value; OnPropertyChanged(); }
    }

    public SearchVM(User user)
    {
        _user = user;
        _searchService = new SearchService();
    }

    public async void TimerSearch(string text)
    {
        var response = await _searchService.SearchUser(text);

        var data = response.ResponseData;

        SearchedUsers = data.ToObservableCollection(); 
    }
}

bindig 로그인

System.Windows.Data Warning: 67 : BindingExpression (hash=11842506): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=11842506): Found data context element: TextBlock (hash=56511253) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=11842506): Activate with root item SearchVM (hash=52539597)
System.Windows.Data Warning: 108 : BindingExpression (hash=11842506):   At level 0 - for SearchVM.SearchedUsers found accessor RuntimePropertyInfo(SearchedUsers)
System.Windows.Data Warning: 104 : BindingExpression (hash=11842506): Replace item at level 0 with SearchVM (hash=52539597), using accessor RuntimePropertyInfo(SearchedUsers)
System.Windows.Data Warning: 101 : BindingExpression (hash=11842506): GetValue at level 0 from SearchVM (hash=52539597) using RuntimePropertyInfo(SearchedUsers): ObservableCollection`1 (hash=49313939 Count=0)
System.Windows.Data Warning: 108 : BindingExpression (hash=11842506):   At level 1 - for ObservableCollection`1.Count found accessor RuntimePropertyInfo(Count)
System.Windows.Data Warning: 104 : BindingExpression (hash=11842506): Replace item at level 1 with ObservableCollection`1 (hash=49313939 Count=0), using accessor RuntimePropertyInfo(Count)
System.Windows.Data Warning: 101 : BindingExpression (hash=11842506): GetValue at level 1 from ObservableCollection`1 (hash=49313939 Count=0) using RuntimePropertyInfo(Count): '0'
System.Windows.Data Warning: 80 : BindingExpression (hash=11842506): TransferValue - got raw value '0'
System.Windows.Data Warning: 84 : BindingExpression (hash=11842506): TransferValue - implicit converter produced '0'
System.Windows.Data Warning: 89 : BindingExpression (hash=11842506): TransferValue - using final value '0'
System.Windows.Data Warning: 56 : Created BindingExpression (hash=29516363) for Binding (hash=55475379) BindingExpression:Path=SearchedUsers.Count; DataItem=null; 
System.Windows.Data Warning: 58 :  Path: 'SearchedUsers.Count'
System.Windows.Data Warning: 60 : BindingExpression (hash=29516363): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=29516363): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=29516363): Attach to System.Windows.Controls.TextBlock.Text (hash=35990092)
System.Windows.Data Warning: 67 : BindingExpression (hash=29516363): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=29516363): Found data context element: TextBlock (hash=35990092) (OK)
System.Windows.Data Warning: 71 : BindingExpression (hash=29516363): DataContext is null
System.Windows.Data Warning: 65 : BindingExpression (hash=29516363): Resolve source deferred
System.Windows.Data Warning: 67 : BindingExpression (hash=29516363): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=29516363): Found data context element: TextBlock (hash=35990092) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=29516363): Activate with root item SearchVM (hash=64320678)
System.Windows.Data Warning: 107 : BindingExpression (hash=29516363):   At level 0 using cached accessor for SearchVM.SearchedUsers: RuntimePropertyInfo(SearchedUsers)
System.Windows.Data Warning: 104 : BindingExpression (hash=29516363): Replace item at level 0 with SearchVM (hash=64320678), using accessor RuntimePropertyInfo(SearchedUsers)
System.Windows.Data Warning: 101 : BindingExpression (hash=29516363): GetValue at level 0 from SearchVM (hash=64320678) using RuntimePropertyInfo(SearchedUsers): ObservableCollection`1 (hash=42592467 Count=0)
System.Windows.Data Warning: 107 : BindingExpression (hash=29516363):   At level 1 using cached accessor for ObservableCollection`1.Count: RuntimePropertyInfo(Count)
System.Windows.Data Warning: 104 : BindingExpression (hash=29516363): Replace item at level 1 with ObservableCollection`1 (hash=42592467 Count=0), using accessor RuntimePropertyInfo(Count)
System.Windows.Data Warning: 101 : BindingExpression (hash=29516363): GetValue at level 1 from ObservableCollection`1 (hash=42592467 Count=0) using RuntimePropertyInfo(Count): '0'
System.Windows.Data Warning: 80 : BindingExpression (hash=29516363): TransferValue - got raw value '0'
System.Windows.Data Warning: 84 : BindingExpression (hash=29516363): TransferValue - implicit converter produced '0'
System.Windows.Data Warning: 89 : BindingExpression (hash=29516363): TransferValue - using final value '0'
System.Windows.Data Warning: 95 : BindingExpression (hash=11842506): Got PropertyChanged event from SearchVM (hash=52539597)
System.Windows.Data Warning: 101 : BindingExpression (hash=11842506): GetValue at level 0 from SearchVM (hash=52539597) using RuntimePropertyInfo(SearchedUsers): ObservableCollection`1 (hash=53428882 Count=2)
System.Windows.Data Warning: 105 : BindingExpression (hash=11842506):   Item at level 1 has same type - reuse accessor RuntimePropertyInfo(Count)
System.Windows.Data Warning: 104 : BindingExpression (hash=11842506): Replace item at level 1 with ObservableCollection`1 (hash=53428882 Count=2), using accessor RuntimePropertyInfo(Count)
System.Windows.Data Warning: 101 : BindingExpression (hash=11842506): GetValue at level 1 from ObservableCollection`1 (hash=53428882 Count=2) using RuntimePropertyInfo(Count): '2'
System.Windows.Data Warning: 80 : BindingExpression (hash=11842506): TransferValue - got raw value '2'
System.Windows.Data Warning: 84 : BindingExpression (hash=11842506): TransferValue - implicit converter produced '2'
System.Windows.Data Warning: 89 : BindingExpression (hash=11842506): TransferValue - using final value '2'

안녕하세요! 을 때가 있는 경우 함수를 호출하는 SearchView.TimerSearch("x")

나는 생각을 정렬 보여줍니다 카운트에서 반환된 SearchView.TimerSearch("x")

하지만 그것은 작동하지 않는 방법을 내가 기대하고 있습니다.

로그인딩을 말한 그것을 변경 2. 하지만 그것을 보여줍'0'그리고 변경되지 않!

누군가의 도움을 받을 수 있는가?

data-binding mvvm wpf
2021-11-22 16:05:54
1

최고의 응답

1

당신은 바인딩을 중첩 Count 시설의 컬렉션입니다. 그러나 때문에 교체하는 완전한 컬렉션 Count 속성을 효과적으로는 변하지 않으로 컬렉션에 인스턴스의 변화가 감지되지 않으로 Binding. 즉, Binding 이 듣고 Count 변경된 이벤트이지만,이 이벤트가 발생하지 않기 때문에 컬렉션에 인스턴스 자체가 변경되었다.
그것은 일반적으로 추천을 항상 사용하여 동일한 컬렉션에서 인스턴스는 데이터 바인딩 context:맑은 컬렉션과 추가로 새로운 범위의 항목입니다.

다음은 당신의 비동기 방법 TimerSearch:그것은 현재 없이 기다리고 반환하지 않는 Task!
기억하십시오: async void 허용을 위한 이벤트를 처리기입니다. 그렇지 않으면 void 방법은 반드시 항상 변환로 돌아 Task.

할 때 await 는 방법을해야 합니다 await 전체 호출 트리(는 모든 발신자도): async Task 다.

// Make method return Task
public async Task TimerSearchAsync(string text)
{
    var response = await _searchService.SearchUser(text);

    SearchedUsers.Clear();
    foreach (var data in response.ResponseData)
    {
       SearchedUsers.Add(data);
    }
}

는 경우 response.ResponseData 반환 List<T>교체할 수 있습니다 foreach 루프의 전화 List<T>.ForEach 메서드를 압축하는 코드:

response.ResponseData.ForEach(SearchedUsers.Add);

다음 변환,호출하는 방법을 반환 TaskawaitTimerSearch 방법 전화:

// Declare method async and return a Task
public async Task TimerSearcAsynch(string text)
{
    await _searchVM.TimerSearchAsync(text);
}

마지막으로,설정하지 않은 DataContext 에 있습니다. 이 한계는 사용자의 통제하에서만 사용한 특별한다.

일반적으로,당신은 보기 없 모델 등에 대한 각각의 제어,이하지 않는 방법을 디자인하 제어합니다. 제품 개발에서 독립적 모델을 볼 클래스입니다.
귀하의 경우에,제어있을 것입 UserCount 종속성 제공,당신이 나중에 바인딩할 수 있는 외부 전망의 모델입니다. 내부적으로, TextBox 것에 바인딩 UserCount 종속성을 제공합니다.

2021-11-22 17:27:32

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................