我如何刷新MVVM WPF中的UI

我的项目基于MVVM模式。

我已经构建了一个显示我的文件系统的树视图。 每个文件夹都有一个用于选择当前文件夹的复选框。 选择过程需要一些时间,当操作运行时,有一个按钮被禁用,在操作结束时,我正在启用按钮。

我的问题是,当按钮被禁用时,我立即看到它。 但是,当按钮返回到启用模式时,我必须执行一些操作(如单击鼠标)以查看启用的按钮。

如何确保在按钮启用后UI将立即更新?

这些是我的按钮:

<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}" />

在我的ViewModel中,我添加了下面的代码:

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");
        }
    }
}

UI更新发生在我执行鼠标单击或任何击键之后。

这是禁用和启用按钮的操作的代码:

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;
}

我能做什么?


您可以调整控件Binding mode TwoWaytriggers with PropertyChanged定义triggers with PropertyChanged

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

好的,我找到了解决方案。
我尝试了一切都没有成功,最终我发现这个线程:刷新WPF命令

我已经使用CommandManager.InvalidateRequerySuggested()

它的作品。

谢谢你的帮助


下面是一个代码示例,说明如何使用发送消息更新UI的INotifyPropertyChanged方法设置ViewModel:

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
}
链接地址: http://www.djcxy.com/p/56129.html

上一篇: How can I refresh the UI in MVVM WPF

下一篇: WPF Using commands to slow to update UI controls