'Remove a BlurEffect from control WPF C#
I have a border containing a textblock, which has a blur effect applied to it:
<Border Grid.Row="2">
<Border.Effect>
<BlurEffect Radius="5" />
</Border.Effect>
<TextBlock/>
</Border>
I want to remove this BlurEffect on a button press, but I cannot find how. I have tried to set the radius to 0, but the text is still blurred.
Any help would be greatly appreciated.
Solution 1:[1]
One option is to set the radius by a Setter in a Trigger or DataTrigger. This however is a bit complicated since you can't add a Stye inside the Effect. (See also the link provided by @silverfighter in the comments.)
One easy solution is to bind the Radius
to a property and change it from code like so:
<Border>
<Border.Effect>
<BlurEffect Radius="{Binding MyBlurRadius}" />
</Border.Effect>
</Border>
Your ViewModel then needs to implement the INotifyPropertyChanged
interface and provide a property MyBlurRadius
which calls OnPropertyChanged()
in its setter:
public MyViewModel : INotifyPropertyChanged
{
private int _myBlurRadius;
public int MyBlurRadius
{
get { return _myBlurRadius; }
set
{
if (_myBlurRadius == value)
return;
_myBlurRadius = value;
OnPropertyChanged();
}
}
}
As log as there is no other setter rechanging the radius, the blur will disapear once the radius is set to 0.
The drawback of this soultion is that you have to deal with visualisation from inside your code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Felix |