开发者

c# WPF how to produce a flashy warning

开发者 https://www.devze.com 2023-03-17 05:16 出处:网络
I\'m learning WPF, so bear with me. I would like to have my WPF application flas开发者_JAVA百科h in the user\'s face if a certain event is fired.

I'm learning WPF, so bear with me.

I would like to have my WPF application flas开发者_JAVA百科h in the user's face if a certain event is fired.

What is the best way to "notify" the user? I really want the user to react!

Cheers, Patrick

Environment: Windows7/64bit/.Net4


If you want the user to react you can force them to by simply opening a modal dialogue. The most lightweight of which being the MessageBox. You can also create normal modal windows using their ShowDialog method, you can make those windows as "fancy" as you want by getting rid of their normal appearance. This is achieved by setting the WindowStyle to None and AllowsTransparency to true, this will remove all the frame elements, so the window is now pure content.

Popups are handy for non-modal notifications and they already are content-only, but setting their AllowsTransparency to true may also be desired if you want rounded corners for example.


Best is entirely subjective and depends on many context variables but here is how I do it MVVM style.

In your main view model, define a property

pubic ObservableCollection<AlertViewModel"> Alerts { get; private set; }

in my case the AlertViewModel has only a "Message" property and a "Dismiss" RelayCommand.

In the XAML of your main view add

<Grid>
    <all of my other other view controls>
    <ItemsControl x:Name="AlertsControl" Opacity="50" ItemsSource="{Binding Alerts}"/>
</Grid>

Make sure it is the last item in the main container of your main view. This ensures it has the highest z order and will appear on top of all other controls.

Here is the data template for this view model

<DataTemplate DataType="{x:Type vm:AlertViewModel}">
   <Border CornerRadius="10" Margin="3" Background="Red">
     <Grid>
       <Grid.ColumnDefinitions>
         <ColumnDefinition Width="75"></ColumnDefinition>
         <ColumnDefinition></ColumnDefinition>
       </Grid.ColumnDefinitions>
       <Button Margin="10" Grid.Column="0" 
               Command="{Binding ElementName=theWindow, Path=DataContext.DismissAlarmCommand}" 
               CommandParameter="{Binding}">Dismiss</Button>
       <TextBlock  Foreground="White" FontWeight="ExtraBold" Grid.Column="1" 
               Text="{Binding Message}"  FontSize="20" 
               VerticalAlignment="Center" HorizontalAlignment="Left"></TextBlock>
     </Grid>
   </Border>
</DataTemplate>

Now,

Alerts.Add( new AlertViewModel() { Message = "Danger Will Robinson! Danger!" } );

Will pop a Bright red alert box onto the top of your main form. It does not go away until the user presses "Dismiss"

If you want it to flash or fade in and out or bounce up and down you can add animation in the data template.

You can use a Converter or data to Enable/Disable the rest of the controls in the app byt binding to AlertsControl.HasItems

Good luck.

0

精彩评论

暂无评论...
验证码 换一张
取 消