Setter Class

July 28, 2014

Applies a value to a property in a Style.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

public sealed class Setter : SetterBase

The Setter type exposes the following members.

  NameDescription
Public methodSetter()Initializes a new instance of the Setter class.
Public methodSetter(DependencyProperty, Object)Initializes a new instance of the Setter class with the specified property and value.
Top

  NameDescription
Public propertyDispatcherGets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Public propertyIsSealedGets a value that indicates whether this object is in an immutable state. (Inherited from SetterBase.)
Public propertyPropertyGets or sets the property to apply the Value to.
Public propertyValueGets or sets the value to apply to the property that is specified by the Setter.
Top

  NameDescription
Public methodCheckAccessDetermines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public methodClearValueClears the local value of a dependency property. (Inherited from DependencyObject.)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetAnimationBaseValueReturns any base value established for a Windows Phone dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetValueReturns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodReadLocalValueReturns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Public methodSetValueSets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

A Style contains a collection of one or more Setter objects. Each Setter has a Property and a Value. The Property is the property of the element the style is applied to. The Value is the value that is applied to the property.

You must specify both the Property and Value properties on a Setter or an exception is thrown.

The following example creates two styles: one for a TextBlock and one for a 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. Each style is applied to two instances of a control to create a uniform appearance for each TextBlock and TextBox.


<StackPanel>
  <StackPanel.Resources>
      <!--Create a Style for a TextBlock that uses some 
          values from the Theme Resources for Windows Phone 
          (http://msdn.microsoft.com/en-us/library/ff769552(v=VS.92).aspx).-->
      <Style TargetType="TextBlock" x:Key="TextBlockStyle">
              <Setter Property="Foreground" Value="{StaticResource PhoneSubtleColor}"/>
              <Setter Property="Margin" Value="{StaticResource PhoneMargin}"/>
              <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
      </Style>

      <!--Create a Style for a TextBox that uses some 
          values from the Theme Resources for Windows Phone 
          (http://msdn.microsoft.com/en-us/library/ff769552(v=VS.92).aspx).-->
      <Style TargetType="TextBox" x:Key="TextBoxStyle">
          <Setter Property="Width" Value="300"/>
          <Setter Property="Height" Value="70"/>
          <Setter Property="Margin" Value="{StaticResource PhoneMargin}"/>
          <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/>
          <Setter Property="Background" Value="{StaticResource PhoneAccentColor}" />
    </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}" />
  </StackPanel>
</StackPanel>


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Show:
© 2014 Microsoft