April 29, 2013 10:48 by
Scott
Maybe there is a working solution for this already out there, but I created my own Silverlight Behavior for a basic TextBox Watermark which might be useful.
I wanted to use it like this in my XAML (look at the behaviors tag):
<TextBlock Margin="5">Watermarked textbox:</TextBlock>
<TextBox Margin="5">
<Interactivity:Interaction.Behaviors>
<local:Watermark Text="Watermark" Foreground="LightGray" />
</Interactivity:Interaction.Behaviors>
</TextBox>
The result should be something like this:
To create a Behavior for Silverlight, you must get hold of the System.Windows.Interactivity assembly which ships with Expression Blend. In my system it’s located at:
c:\Program Files (x86)\Microsoft SDKs\Expression\Blend\Silverlight\v4.0\Libraries\System.Windows.Interactivity.dll
And the code for the Behavior:
public class Watermark : Behavior<TextBox>
{
private bool _hasWatermark;
private Brush _textBoxForeground;
public String Text { get; set; }
public Brush Foreground { get; set; }
protected override void OnAttached()
{
_textBoxForeground = AssociatedObject.Foreground;
base.OnAttached();
if (Text != null)
SetWatermarkText();
AssociatedObject.GotFocus += GotFocus;
AssociatedObject.LostFocus += LostFocus;
}
private void LostFocus(object sender, RoutedEventArgs e)
{
if (AssociatedObject.Text.Length == 0)
if (Text != null)
SetWatermarkText();
}
private void GotFocus(object sender, RoutedEventArgs e)
{
if (_hasWatermark)
RemoveWatermarkText();
}
private void RemoveWatermarkText()
{
AssociatedObject.Foreground = _textBoxForeground;
AssociatedObject.Text = "";
_hasWatermark = false;
}
private void SetWatermarkText()
{
AssociatedObject.Foreground = Foreground;
AssociatedObject.Text = Text;
_hasWatermark = true;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.GotFocus -= GotFocus;
AssociatedObject.LostFocus -= LostFocus;
}
}
Like so many Watermark-solutions out there I’m hooking into the GotFocus/LostFocus events and to the work there. Works for me.
March 11, 2013 07:12 by
Scott
In this article we will be seeing how to create Silverlight BlurEffect using Visual studio 2010.
Pixel shader effects in Silverlight allows you to add effects, such as gray scale, red eye removal, pixel brightness, and shadows, to rendered objects. There are two types of Pixel Shader effects in Silverlight. They are BlurEffect and DropShadowEffect. In this we will be seeing about BlurEffect and its properties.
Namespace: System.Windows.Media. Effects
Assembly: System.Windows
BlurEffect:
BlurEffect is used to represent an effect that we can apply to an object that simulates looking at the object through an out-of-focus lens. It is defined by the Radius property.
Radius:
This property is used to specify the amount of blur to apply to an object.
Without BlurEffect:
<Canvas Height="200" Width="200" Background="white">
<Rectangle Height="50" Width="50" Fill="Orange" Canvas.Left="25" Canvas.Top="75"></Rectangle>
<Rectangle Height="50" Width="50" Fill="White" Canvas.Left="75" Canvas.Top="75"></Rectangle>
<Rectangle Height="50" Width="50" Fill="Green" Canvas.Left="125" Canvas.Top="75"></Rectangle>
</Canvas>
With BlurEffect:
<Canvas Height="200" Width="200" Background="white">
<Canvas.Effect>
<BlurEffect Radius="120"></BlurEffect>
</Canvas.Effect>
<Rectangle Height="50" Width="50" Fill="Orange" Canvas.Left="25" Canvas.Top="75"></Rectangle>
<Rectangle Height="50" Width="50" Fill="White" Canvas.Left="75" Canvas.Top="75"></Rectangle>
<Rectangle Height="50" Width="50" Fill="Green" Canvas.Left="125" Canvas.Top="75"></Rectangle>
</Canvas>
Steps Involved:
Creating a Silverlight Application:
- Open Visual Studio 2010.
- Go to File => New => Project.
- Select Silverlight from the Installed templates and choose the Silverlight Application template.
- Enter the Name and choose the location.
- Click OK.
- In the New Silverlight Application wizard check the "Host the Silverlight Application in a new Web site".
- Click OK.
Creating the UI:
Open MainPage.xaml file and replace the code with the following.
<UserControl x:Class="SilverlightBlurEffect.MainPage"
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
xmlns:d=http://schemas.microsoft.com/expression/blend/2008
xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Canvas Height="200" Width="200" Background="white">
<Canvas.Effect>
<BlurEffect Radius="120"></BlurEffect>
</Canvas.Effect>
<Rectangle Height="50" Width="50" Fill="Orange" Canvas.Left="25" Canvas.Top="75"></Rectangle>
<Rectangle Height="50" Width="50" Fill="White" Canvas.Left="75" Canvas.Top="75"></Rectangle>
<Rectangle Height="50" Width="50" Fill="Green" Canvas.Left="125" Canvas.Top="75"></Rectangle>
</Canvas>
</UserControl>
To test it, please just build the solution and hit CTRL+F5