Search code examples
wpfmvvmpopuptimeoutshow-hide

WPF and MVVM. Display and hide a label with a timeout


in my MVVM application, I wish to create an auto-closing popup to notify some information to the users (for example "data changes saved successfully"). so, I placed a label into the form, bound to a VM property. Then, I wish to set my message and cancel it after a delay (1 second). But it seems not to work. the app just wait some time, and shows the final status (ie: when the user push "save" button, the app "waits" for one second, and then the label is empty). any ideas to get it? thanks


Solution

  • Why can't you use normal popup in WPF

    <Popup Margin="10,10,0,13" Name="Popup1" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="194" Height="200" IsOpen="True">  
        <StackPanel>  
            <TextBlock Name="McTextBlock"   
             Background="LightBlue" >  
            This is popup text   
           </TextBlock>  
            <Button Content="This is button on a Pupup" />  
        </StackPanel>  
    

    public void show()
    {
      Popup1.IsOpen = true;
      Thread t = new Thread(hide);
      t.Start();
    }
    
    private void hide() {
      Thread.Sleep(5000);
      Popup1.IsOpen = false;
    }
    

    call show function when you want to show popup