简体   繁体   中英

WPF Control Visibility w/ ICommand & OnPropertyChanged

I have a user window with 3 controls - an 'execute' button, a results control, and a processing control. My goal is to display the processing control after execute is pressed, then hide it when the execute method finishes.

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. The execute method sets Processing to true at the start, then to false when it finishes. The setter of Processing calls OnPropertyChanged. My view model implements 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:

    <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. This will enable Processing control to visible and hide. If your chaining the value of bool Processing in a task or back ground worker make sure you invoke it via dispatcher

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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