Dự án của tôi dựa trên mẫu MVVM.Làm cách nào để làm mới giao diện người dùng trong MVVM WPF
Tôi đã xây dựng chế độ xem dạng cây hiển thị hệ thống tệp của tôi. Mỗi thư mục có một hộp kiểm để chọn thư mục hiện tại. Quá trình lựa chọn mất một chút thời gian, trong khi thao tác chạy, có một nút bị tắt và khi kết thúc thao tác, tôi bật nút.
Vấn đề của tôi là khi nút bị "tắt" tôi thấy ngay lập tức. Tuy nhiên, khi nút quay trở lại chế độ được bật, tôi phải thực hiện một số thao tác (như nhấp chuột) để xem nút được bật.
Làm cách nào để đảm bảo rằng giao diện người dùng sẽ được cập nhật ngay sau khi nút được bật?
Đây là những nút của tôi:
<Button Content="<- Back" Margin="5,0,5,0" Width="80" Height="25"
IsEnabled="{Binding CanMoveToPreviousPage, UpdateSourceTrigger=PropertyChanged}"
Command="{Binding Path=NavigateBackCommand, IsAsync=True}" />
<Button Content="{Binding ButtonNextCaption}" Margin="5,0,5,0" Width="80" Height="25"
IsEnabled="{Binding CanMoveToNextPage, UpdateSourceTrigger=PropertyChanged}"
Command="{Binding Path=NavigateNextCommand, IsAsync=True}" />
Trong tôi ViewModel tôi đã thêm mã này:
public bool CanMoveToNextPage
{
get
{
return this.CurrentPage != null && this.CurrentPage.CanMoveNext;
}
set
{
if (CurrentPage != null)
{
this.CurrentPage.CanMoveNext = value;
OnPropertyChanged("CanMoveToNextPage");
}
}
}
public bool CanMoveToPreviousPage
{
get { return 0 < this.CurrentPageIndex && CurrentPage.CanMoveBack; }
set
{
if (CurrentPage != null)
{
this.CurrentPage.CanMoveBack = value;
OnPropertyChanged("CanMoveToPreviousPage");
}
}
}
Giao diện cập nhật xảy ra ngay sau khi tôi thực hiện một click chuột hoặc bất kỳ tổ hợp phím.
Đây là mã của hành động đó là vô hiệu hóa và kích hoạt các nút:
void bg_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
DecrementDoneCounter();
if (ThreadSafeCouner == 0)//means all bg workers are done
{
UIlimitation(true);
}
}
private int ThreadSafeCouner; // check how many bgworkers run
public void IncrementDoneCounter() { Interlocked.Increment(ref ThreadSafeCouner); }
public void DecrementDoneCounter() { Interlocked.Decrement(ref ThreadSafeCouner); }
void bg_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
IncrementDoneCounter();
UIlimitation(false);
((bgArguments)e.Argument).SelectedDirectory.CanSelected = false;
MarkItems(((bgArguments)e.Argument).SelectedDirectory, ((bgArguments)e.Argument).IsSelect);
((bgArguments)e.Argument).FreeWorkerAllocation();
((bgArguments)e.Argument).SelectedDirectory.CanSelected = true;
}
//this is the enabling action which execute the propeties setters at the upper part of this post
private static void UIlimitation(bool limit)
{
MainWindowViewModel.Instance.CanMoveToNextPage = limit;
MainWindowViewModel.Instance.CanMoveToPreviousPage = limit;
}
tôi có thể làm gì?
thử this.UpdateLayout() hoặc ButtonsParent.UpdateLayout() –
Khi hoạt động của bạn được thực hiện bạn đang nâng cao 'sự kiện OnPropertyChanged' đối với tài sản có liên quan của bạn chịu trách nhiệm bật/tắt nút? –
Khi tôi hoàn thành, tôi sẽ thay đổi thuộc tính đã bật nhưng Đặt {...} và Set {} bao gồm sự kiện OnPropertyChanged. Khi tôi cố gắng để gỡ lỗi tất cả mọi thứ hoạt động perfevt và nút được kích hoạt mà không cần nhấp chuột. Tôi có thể tìm ra vấn đề là gì – Ofir