简体   繁体   English

具有ICommand和OnPropertyChanged的WPF控件可见性

[英]WPF Control Visibility w/ ICommand & OnPropertyChanged

I have a user window with 3 controls - an 'execute' button, a results control, and a processing control. 我有一个带有3个控件的用户窗口 - 一个'执行'按钮,一个结果控件和一个处理控件。 My goal is to display the processing control after execute is pressed, then hide it when the execute method finishes. 我的目标是在按下execute后显示处理控件,然后在execute方法完成时隐藏它。

However, the processing control does not display when I assumed it would... instead it only displays when (if) a callback function that creates another window prompting for user input is called. 但是,当我假设处理控件时,处理控件不显示...而是仅显示何时(如果)调用创建另一个提示用户输入的窗口的回调函数。

The processing control has its visibility bound to a bool Processing in my viewmodel via BoolToVis converter. 处理控件的可见性通过BoolToVis转换器绑定到我的viewmodel中的bool Processing The execute method sets Processing to true at the start, then to false when it finishes. execute方法在开始时将Processing设置为true,然后在完成时将其设置为false。 The setter of Processing calls OnPropertyChanged. Processing的setter调用OnPropertyChanged. My view model implements INotifyPropertyChanged. 我的视图模型实现了INotifyPropertyChanged.

    private bool _processing;
    public bool Processing
    {
        get
        { return _processing; }
        set
        {
            _processing = value;
            this.OnPropertyChanged("Processing");
        }
    }

    private RelayCommand _search;
    public RelayCommand Search
    {
        get { return _search ?? (_search = new RelayCommand(p => OnSearch(), p => CanSearch())); }
    }
    private void OnSearch()
    {
        this.Processing = true;

        DoWork(delegate callBack);

        this.Processing = false;
    }

And some of the XAML: 还有一些XAML:

    <BooleanToVisibilityConverter x:Key="BoolToVis" />

    <me:ProcessingControl Visibility="{Binding Path=Processing, Converter={StaticResource ResourceKey=BoolToVis}}"/>

Use Task or Background Worker to do DoWork, set Processing=true before starting the task or Background worker and make it false at the end of the task. 使用Task或Background Worker执行DoWork,在启动任务或后台工作程序之前设置Processing = true,并在任务结束时将其设置为false。 This will enable Processing control to visible and hide. 这将使Processing控件可见并隐藏。 If your chaining the value of bool Processing in a task or back ground worker make sure you invoke it via dispatcher 如果您在任务或后台工作人员中链接bool处理的值,请确保通过调度程序调用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM