WPF material design Snackbar duration

StealthRT picture StealthRT · Jan 2, 2017 · Viewed 7.3k times · Source

Hey all I am trying to figure out how I can empliment a duration for the Snackbar that comes with the ButchersBoy Material Design In Xaml Toolkit found here and here.

There is no where that states if I can use a duration or not so maybe I am just looking over it within the code? There has got to be a parameter somewhere that allows this?

My current code is this (code behind):

items.Snackbar.MessageQueue.Enqueue("Wow, easy!")

And the XAML:

<materialDesign:Snackbar 
        HorizontalAlignment="Stretch" 
        MessageQueue="{materialDesign:MessageQueue}" 
        x:Name="Snackbar" 
        Grid.Row="1" 
        Grid.ColumnSpan="2" />

Which does work and shows "Wow, easy!" but it goes away too quickly and so that is why I am trying to find a way to do a duration on it.

Answer

James Willock picture James Willock · Jan 3, 2017

SnackbarMessageQueue has the following constructor:

public SnackbarMessageQueue(TimeSpan messageDuration)

So you can create the message queue in a view model, and assign, such as:

<materialDesign:Snackbar MessageQueue="{Binding MyCustomMessageQueue}" />

Or, using code behind, name your control:

<materialDesign:Snackbar x:Name="MySnackbar" />

And then in code-behind you can assign a new snackbar:

var myMessageQueue = new SnackbarMessageQueue(TimeSpan.FromMilliseconds(8000));
MySnackbar.MessageQueue = myMessageQueue;

In the future we could add this to the markup extension used in your example , feel free to raise a request on GitHub for this.