-1

I'm trying to develop a busy Window as said here and here. I want a grid to be visible whenver I need it to be visible, for example while I'm doing a long task.

I did this til now:

The XAML:

<Window x:Class="LoadingWindow2.MainWindow"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:loadingWindow2="clr-namespace:LoadingWindow2"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <loadingWindow2:BoolToVisibilityConverter x:Key="boolConverter"/>
</Window.Resources>
<Grid>
    <Label HorizontalAlignment="Right">
        <Hyperlink Command="{Binding Path=DoSomething}">Do Something</Hyperlink>
    </Label>
    <Border BorderBrush="Black" BorderThickness="1" Background="#80000000" Visibility="{Binding IsBusy, Converter={StaticResource boolConverter}}" Grid.RowSpan="3">
        <Grid>
            <TextBlock Margin="0" TextWrapping="Wrap" Text="Please Wait..." HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" FontWeight="Bold" Foreground="#7EFFFFFF"/>
        </Grid>
    </Border>
</Grid>

My .cs:

The BusyViewModel.cs:

public class BusyViewModel : INotifyPropertyChanged
{
    private ICommand doSomethingCommand;

    public event PropertyChangedEventHandler PropertyChanged;

    public bool IsBusy { get; set; }

    public ICommand DoSomething
    {
        get { return doSomethingCommand ?? (doSomethingCommand = new DelegateCommand(LongRunningTask)); }
    }

    private void LongRunningTask()
    {
        var task = new Task(ComputeResults);
        task.Start();
    }

    private void ComputeResults()
    {
        this.IsBusy = true;
        Thread.Sleep(5000);
        this.IsBusy = false;
    }
}

The DelegateCommand.cs:

public class DelegateCommand : ICommand
{
    private readonly Action executeMethod;
    private readonly Func<bool> canExecuteMethod;

    public DelegateCommand(Action executeMethod)
        : this(executeMethod, () => true)
    {
    }

    public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod)
    {
        if (executeMethod == null)
            throw new ArgumentNullException("executeMethod");
        if (canExecuteMethod == null)
            throw new ArgumentNullException("canExecuteMethod");

        this.executeMethod = executeMethod;
        this.canExecuteMethod = canExecuteMethod;
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public bool CanExecute(object stupid)
    {
        return CanExecute();
    }

    public bool CanExecute()
    {
        return canExecuteMethod();
    }

    public void Execute(object parameter)
    {
        Execute();
    }

    public void Execute()
    {
        executeMethod();
    }

    public void OnCanExecuteChanged()
    {
        CommandManager.InvalidateRequerySuggested();
    }
}

MainWindow.xaml.cs:

 public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new BusyViewModel();
    }

I downloaded the source code from the first link I copied, and the busy Grid is showing. But in my case... is NOT!!! What I'm doing wrong here?

EDIT: I deleted the Converter as suggested. But it's not working yet... I add my `MainWindow.xaml.cs"

The whole source: here

6
  • Why not just use the code you downloaded and is working?
    – paparazzo
    Jun 20, 2014 at 9:53
  • I have to insert this code in a big project. And if it doesn't work on a little project... why should work in the big one? And... the big question: Why is not working if it's the same?
    – Sonhja
    Jun 20, 2014 at 9:56
  • Why not use the code that is working?
    – paparazzo
    Jun 20, 2014 at 10:01
  • -1 The dropbox link to your source is NOT the same as what you posted. The posted code does not have the error.
    – paparazzo
    Jun 20, 2014 at 10:56
  • @Blam as I said in my comments with MAVBT's solution, I'm using HIS code, not mine anymore. But using that code I had problems anyway because, as MAVBT said, I was not sending the OnPropertyChanged event. Same code with that event, is working. But I uploaded what MAVBT was suggesting.
    – Sonhja
    Jun 20, 2014 at 11:03

1 Answer 1

2

There is a converter available from WPF already, the "booleanToVisibilityConverter" that does the job.

See http://msdn.microsoft.com/de-de/library/system.windows.controls.booleantovisibilityconverter%28v=vs.110%29.aspx

Edit your xaml like this:

<Window x:Class="LoadingWindow2.MainWindow"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:loadingWindow2="clr-namespace:LoadingWindow2"
   Title="MainWindow" Height="350" Width="525">
    <Window.Resources>    
        <BooleanToVisibilityConverter x:Key="boolConverter"/>
    </Window.Resources>
    <Grid x:Name="ROOT">
        <Label HorizontalAlignment="Right">
            <Hyperlink Command="{Binding Path=DoSomething}">Do Something</Hyperlink>
   </Label>
   <Border BorderBrush="Black" BorderThickness="1" Background="#80000000" Visibility="{Binding Path=IsBusy, Converter={StaticResource boolConverter}}" Grid.RowSpan="3">
       <Grid>
           <TextBlock Margin="0" TextWrapping="Wrap" Text="Please Wait..." 
            horizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" FontWeight="Bold" Foreground="#7EFFFFFF"/>
   </Border>
</Grid>

EDIT: Implementation of BusyViewMOdel

public class BusyViewModel : INotifyPropertyChanged
{
private ICommand doSomethingCommand;

public event PropertyChangedEventHandler PropertyChanged;

private bool _isBusy = false;
public bool IsBusy 
{
    get { return _isBusy; }
    set
    {
       _isBusy = value;
       OnPropertyChanged("IsBusy");
    }
}

// Create the OnPropertyChanged method to raise the event
  protected void OnPropertyChanged(string name)
  {
      PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null)
      {
          handler(this, new PropertyChangedEventArgs(name));
      }
  }


public ICommand DoSomething
{
    get { return doSomethingCommand ?? (doSomethingCommand = new DelegateCommand(LongRunningTask)); }
}

private void LongRunningTask()
{
    var task = new Task(ComputeResults);
    task.Start();
}

private void ComputeResults()
{
    this.IsBusy = true;
    Thread.Sleep(5000);
    this.IsBusy = false;
}
}

See http://msdn.microsoft.com/de-de/library/ms743695%28v=vs.110%29.aspx


EDIT: Try to set the data context on the root grid by giving the grid a name and instead of this.DataContext = ... in public MainWindow() ...do ROOT.DataContext = .... See updated xaml!


EDIT: Got it working. See this code of class BusyViewModel.

    private void LongRunningTask()
    {
        var task = new Task(ComputeResults);
        task.Start();
    }

    private void ComputeResults()
    {
        this.IsBusy = true; // you did _isBusy = true. but to invoke OnPropertyChanged you need to use the setter, thus IsBusy! Works now even if set in the worker thread. Put it back to ComputeResults!
        Thread.Sleep(5000);
        this.IsBusy = false;
    }
9
  • Still not working with that... Do you think is because of the Converter?
    – Sonhja
    Jun 20, 2014 at 9:31
  • Ah i think the problem resides somewhere else. Could you please show the code behindo f the mainwindow?
    – MABVT
    Jun 20, 2014 at 9:34
  • 2
    I think that your implementation of the BusyViewModel is not correct. Where to you implement the notification of property changed. See the answer
    – MABVT
    Jun 20, 2014 at 9:38
  • See the updated xaml. ( x:name=ROOT and the update in the converter binding ). The DelegateCommand is not of interest for the visibility concerns.
    – MABVT
    Jun 20, 2014 at 9:48
  • I tried that before, now is always visible... -_- And never reports any change.
    – Sonhja
    Jun 20, 2014 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.