WPF Attached Behaviour: Blur To Focus Attention

Hi, for those dont know yet, I develop using WPF. This week I found a good idea from Walt Ritscher. In his blog post is showed how to use WPF blur effect to make a dialog window more highlighted. This is a designer’s work because I never thought make use of blur effect like that (thanks Walt for your idea). But solution presented by him is not “MVVM friend". Here, I can’t live without MVVM, so I’ve encapsulated his solution in an attached behaviour:

Code 1. Behaviour

public class CustomEffects
{
    public static bool GetBlurToFocusAttention(DependencyObject obj)
    {
        return (bool)obj.GetValue(BlurToFocusAttentionProperty);
    }

    public static void SetBlurToFocusAttention(DependencyObject obj, bool value)
    {
        obj.SetValue(BlurToFocusAttentionProperty, value);
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc…
    public static readonly DependencyProperty BlurToFocusAttentionProperty =
        DependencyProperty.RegisterAttached("BlurToFocusAttention", typeof(bool), typeof(CustomEffects),
                                            new FrameworkPropertyMetadata(false,
                                                                          new PropertyChangedCallback(
                                                                              OnBlurToFocusAttentionChanged)));

    private static void OnBlurToFocusAttentionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if ((d == null)||(!(d is Window)))
        {
            return;
        }

        var target = (Window)d;
        if ((bool)e.NewValue)
        {
            var blur = new BlurEffect();
            blur.Radius = 5;
            target.Effect = blur;
        }
        else
        {
            target.Effect = null;
        }
    }
}

And to use it I just do:

Code 2. Using Behaviour

<Window x:Class="MeusPacientes.Sistema.Principal"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Persistencia="clr-namespace:MeusPacientes.Persistencia;assembly=MeusPacientes.Persistencia"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    xmlns:xcdg="clr-namespace:Xceed.Wpf.DataGrid;assembly=Xceed.Wpf.DataGrid"
    xmlns:julmar="clr-namespace:JulMar.Windows.Behaviors;assembly=JulMar.Wpf.Helpers"
    xmlns:Views="clr-namespace:Xceed.Wpf.DataGrid.Views;assembly=Xceed.Wpf.DataGrid"
    xmlns:bhv="clr-namespace:MeusPacientes.Sistema.Behaviours"
    xmlns:util="clr-namespace:MeusPacientes.Sistema.Util"
    Background="{StaticResource FundoJanela}"
    bhv:CustomEffects.BlurToFocusAttention="{Binding IsShowingDialog}"  
    Icon="./Imagens/programa.ico"    
    Loaded="Window_Loaded"
    Title="Principal" WindowStyle="None" ResizeMode="NoResize">

<!—content of Window –>

</Window>

 

On Figure 1 we can see before blur and on Figure 2 after blur behaviour.

Figure 1. Not using Blur behaviour

image

Figura 2. Using blur behaviour.

image

PS.: These are images from software I’m developing…

Esse post foi publicado em WPF. Bookmark o link permanente.

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair / Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair / Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair / Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair / Alterar )

Conectando a %s