Question

Developing a WPF application using MVVM, I have hit a point where I've got a "save" button on an editable datagrid which does behind the scenes processing but doesn't actually update anything in the UI, so the user has no way of knowing that the save has been successful.

I'm pretty new to WPF, and I presumed there would be a simple flash message control that one could use to notify the user of success and then faded away without them having to do anything. But it seems there's nothing in vanilla WPF that can do this, and there don't seem to be a lot of custom solutions either.

I don't want to use any kind of messagebox because it forces the user to take an action to dismiss the alert - I need something that breifly flashes a message without interfering with their workflow. I'm after something a bit like the JavaScript Toastr library -

http://codeseven.github.io/toastr/demo.html

can anyone either point me at an existing control for this, or where I might start at building one?

Was it helpful?

Solution

I think you don't need any third party controls. You always may create a custom control, paste it in a layout and bind layouts Visibility property to your view model. Another option is to use StatusBar to notify clients like in Word or VisualStudio. There is a just brief example:

Somewhere in xaml:

// ..
<StatusBar DockPanel.Dock="Bottom">
    <StatusBarItem>
        <Label Content="{Binding Message}"></Label>
    </StatusBarItem>
</StatusBar>
// ..

Somewhere in your code (I like to use async/await with WPF):

// ..
statusBarViewModel.Message = "Processing the file..."; // assumed that you bind this view model to the view
await DoWork(); // do much work
await statusBarViewModel.ShowMessageAndHide("File saved"); // show final message and hide it after some time
// ..

And StatusBarViewModel:

public class StatusBarViewModel : INotifyPropertyChanged
{

    private string message = string.Empty;

    public string Message
    {
        get { return message; }
        set
        {
            if (value == message) return;
            message = value;
            OnPropertyChanged();
        }
    }

    public async Task ShowMessageAndHide(string message)
    {
        Message = message;
        await Task.Delay(5000);
        Message = string.Empty;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

}

OTHER TIPS

The way I do simple notifications about the end of processing in WPF is by displaying a tooltip. You can open the tooltip from code:

https://stackoverflow.com/a/1911721/3391743

ToolTip tooltip = new ToolTip{ Content = "My Tooltip" };
NameTextBox.ToolTip = tooltip;
tooltip.IsOpen = true;

Then you can use a timer like here: https://stackoverflow.com/a/1091753/3391743

As for IImplementPropertyChanged, there's a nice attribute [PropertyChanged] in Fody Property Changed assembly available via NuGet, which does all the plumbing for you.

Look here for reference on Fody: https://github.com/Fody/PropertyChanged

You could use the "NotifyIcon" from hardcodet to achieve a toast-like notification in WPF.

On a side note, in order for your UI to update, the binding engine will need to know when something has changed. In .NET, the INotifyPropertyChanged interface serves this purpose, and you'll need to implement that on the members you want to update in your UI when they are changed.

Here is a SO post which covers how to implement it.

There are a couple of parts to what you are asking:

  • To have code that is executed "behind the scenes", it sounds like you are asking about asynchronous programming. See async/await.
  • To have a custom visual that can interact with the view-model, you should take a look at Microsoft Prism's IInteractionRequest implementation.

Here is a good example of a codeproject post talking about how to do this with the Microsoft Prism framework, however you can adapt the code to work for you if you are not using that framework.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top