Setter.Value Property
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Gets or sets the value to apply to the property that is specified by the Setter.
Namespace: System.Windows
Assembly: System.Windows (in System.Windows.dll)
Syntax
'Declaration
Public Property Value As Object
public Object Value { get; set; }
<Setter ...>
<Setter.Value>
objectValue
</Setter.Value>
</Setter>
<Setter Value="attributeValue"/>
-or-
<Setter Value="extensionUsage"/>
XAML Values
objectValue
An object element that specifies the value of the property being set.attributeValue
A string that specifies the attribute value of the property being set. The format and interpretation of the string provided for the attribute value depends on the property being set.extensionUsage
One of several possible extension mechanisms that specify the property value to be obtained from an existing value. The most common of these is StaticResource.
Whether to use attribute syntax or property element syntax depends both on the property type of the property being set, as well as whether you choose to use extension mechanisms such as referencing existing resources. For example, property element syntax might be required if you want the style to define a new ImageBrush as the value for a property that takes a Brush property type, but attribute syntax could be used if you chose a SolidColorBrush instead, or referenced an existing ImageBrush with a StaticResource usage.
Generally, objectValue is a single object element, but multiple object elements are technically possible in the usage if you are setting a dependency property that takes an implicit collection type.
Property Value
Type: System.Object
The value to apply to the property that is specified by the Setter.
Remarks
Note that you must specify both the Property and Value properties on a Setter or an exception is thrown.
You cannot change this property if IsSealed is true.
Examples
The following example creates two styles: one for a TextBlock and one for a TextBox. Each style is applied to two instances of a control to create a uniform appearance for each TextBlock and TextBox. The style for the TextBlock sets the Foreground, FontSize, and VerticalAlignment properties. The style for the TextBox sets the Width, Height, Margin, Background, and FontSize properties. Notice that the setter for the Background of the TextBox uses the property element syntax for the Value so that the Background can use a LinearGradientBrush.
<StackPanel>
<StackPanel.Resources>
<!--Create a Style for a TextBlock to specify that the
Foreground equals Navy, FontSize equals 14, and
VerticalAlignment equals Botton.-->
<Style TargetType="TextBlock" x:Key="TextBlockStyle">
<Setter Property="Foreground" Value="Navy"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
</Style>
<!--Create a Style for a TextBlock that specifies that
the Width is 200, Height is 20, Margin is 4,
Background is LightBlue, and FontSize is 14.-->
<Style TargetType="TextBox" x:Key="TextBoxStyle">
<Setter Property="Width" Value="200"/>
<Setter Property="Height" Value="30"/>
<Setter Property="Margin" Value="4"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
<GradientStop Color="White" Offset="0.0"/>
<GradientStop Color="LightBlue" Offset="0.5"/>
<GradientStop Color="Navy" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
</StackPanel.Resources>
<!--Apply the TextBlockStyle and TextBoxStyle to each
TextBlock and TextBox, respectively.-->
<StackPanel Orientation="Horizontal">
<TextBlock Style="{StaticResource TextBlockStyle}">
First Name:
</TextBlock>
<TextBox Style="{StaticResource TextBoxStyle}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Style="{StaticResource TextBlockStyle}">
Last Name:
</TextBlock>
<TextBox Style="{StaticResource TextBoxStyle}"
Margin="6,4,4,4"/>
</StackPanel>
</StackPanel>
Data Bound Style Values
The following Silverlight 5 example creates an implicit TextBlock style that assigns a binding markup expression to a Setter.Value property. The binding retrieves its value from a property of the current DataContext, which in this case is a simple UserSettings object. In this way, the TextBlock in the example is automatically displayed with the user's preferred FontSize setting. A TextBox bound to the same source enables users to modify the setting and see the change applied immediately.
<UserControl x:Class="SL5DataBindingFeatures.StyleTestPage"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel x:Name="LayoutRoot">
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="{Binding FontSize}"/>
</Style>
</StackPanel.Resources>
<TextBlock Text="FontSize"/>
<TextBox Text="{Binding FontSize, Mode=TwoWay}"
TextChanged="TextBox_TextChanged"/>
</StackPanel>
</UserControl>
Imports System.ComponentModel
Partial Public Class StyleTestPage
Inherits UserControl
Public Sub New()
InitializeComponent()
DataContext = New UserSettings() With {.FontSize = 35}
End Sub
Private Sub TextBox_TextChanged(sender As System.Object,
e As TextChangedEventArgs)
' Update the data source whenever the text box value changes.
CType(sender, TextBox) _
.GetBindingExpression(TextBox.TextProperty).UpdateSource()
End Sub
End Class
Public Class UserSettings
Implements INotifyPropertyChanged
Private _fontSize As Double
Public Property FontSize As Double
Get
Return _fontSize
End Get
Set(value As Double)
_fontSize = value
RaiseEvent PropertyChanged(
Me, New PropertyChangedEventArgs("FontSize"))
End Set
End Property
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
End Class
using System;
using System.ComponentModel;
using System.Windows.Controls;
public partial class StyleTestPage : UserControl
{
public StyleTestPage()
{
InitializeComponent();
DataContext = new UserSettings() { FontSize = 35 };
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
// Update the data source whenever the text box value changes.
(sender as TextBox)
.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
}
public class UserSettings : INotifyPropertyChanged
{
private Double _fontSize;
public Double FontSize
{
get { return _fontSize; }
set
{
_fontSize = value;
PropertyChanged(this, new PropertyChangedEventArgs("FontSize"));
}
}
// Initialize to an empty delegate so that PropertyChanged is never null.
public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.