2012-10-02 610 views
5

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="&lt;- 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ì?

+1

thử this.UpdateLayout() hoặc ButtonsParent.UpdateLayout() –

+0

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? –

+0

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

Trả lời

5

Bạn có thể điều chỉnh về kiểm soát của bạn Binding mode TwoWay và xác định triggers with PropertyChanged

{Binding ElementName=.., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged} 
+0

Tôi đã làm nhưng vấn đề vẫn tồn tại. – Ofir

+0

Mô hình MVVM sẽ triển khai INotifyPropertyChanged, để gửi thông báo đến các điều khiển –

+0

Tôi đã triển khai nó – Ofir

1

Dưới đây là một ví dụ code như thế nào bạn có thể thiết lập ViewModel của bạn với các phương pháp INotifyPropertyChanged gửi tin nhắn để cập nhật giao diện người dùng:

public class MyViewModel : INotifyPropertyChanged 
{ 
    /******************************************************/ 
    /* Property that you have created two-way binding for */ 
    /******************************************************/ 
    private double _myProperty 
    public double MyProperty 
    { 
     get { return _myProperty; } 
     set 
     { 
      _myProperty = value; 

      OnNotifyPropertyChanged("MyProperty"); 
     } 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnNotifyPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    #endregion INotifyPropertyChanged Members 
} 
+1

Xin chào, Dự án của tôi đã chứa mã này. – Ofir

+0

Đảm bảo giá trị của OnNotifyPropertyChanged ("MyProperty"); đúng. –

1

OK Tôi đã tìm được giải pháp.
tôi đã cố gắng tất cả mọi thứ nhưng không thành công và cuối cùng tôi đã tìm thấy chủ đề này: Refresh WPF Command

Tôi đã sử dụng CommandManager.InvalidateRequerySuggested()

Và tác phẩm của mình.

Nhờ sự giúp đỡ của bạn