Setter.Value Property
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)
<Setter ...> <Setter.Value> objectValue </Setter.Value> </Setter>
<Setter Value="attributeValue"/> -or- <Setter Value="extensionUsage"/>
XAML Values
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.ObjectThe value to apply to the property that is specified by the Setter.
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="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://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
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.