9

我的项目基于 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;
}

我能做些什么?

4

3 回答 3

6

您可以调整您的控制 Binding mode TwoWay并定义triggers with PropertyChanged

{Binding ElementName=.., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}
于 2012-10-02T13:48:45.287 回答
4

好的,我找到了解决方案。
我尝试了一切都没有成功,最终我找到了这个线程: Refresh WPF Command

我用过CommandManager.InvalidateRequerySuggested()

及其作品。

谢谢你的帮助

于 2012-10-03T05:58:33.793 回答
2

下面是一个代码示例,说明如何使用 INotifyPropertyChanged 发送消息以更新 UI 的方法设置 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
}
于 2012-10-02T14:41:19.537 回答