Setter Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Applies a value to a property in a Style or a VisualState.
public ref class Setter sealed : SetterBase
/// [Windows.Foundation.Metadata.Activatable(65536, "Microsoft.UI.Xaml.WinUIContract")]
/// [Windows.Foundation.Metadata.Activatable(Microsoft.UI.Xaml.ISetterFactory, 65536, "Microsoft.UI.Xaml.WinUIContract")]
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class Setter final : SetterBase
[Windows.Foundation.Metadata.Activatable(65536, "Microsoft.UI.Xaml.WinUIContract")]
[Windows.Foundation.Metadata.Activatable(typeof(Microsoft.UI.Xaml.ISetterFactory), 65536, "Microsoft.UI.Xaml.WinUIContract")]
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class Setter : SetterBase
Public NotInheritable Class Setter
Inherits SetterBase
<Setter .../>
- Inheritance
- Attributes
Examples
This example shows how to use Setter
statements in a style for TextBlock elements.
<StackPanel>
<StackPanel.Resources>
<!-- Create a Style for a TextBlock to specify that the
Foreground equals Navy, FontSize equals 14, and
VerticalAlignment equals Bottom. -->
<Style TargetType="TextBlock" x:Key="TextBlockStyle">
<Setter Property="Foreground" Value="Navy"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
</Style>
</StackPanel.Resources>
<!-- Apply the TextBlockStyle to 2 different TextBlocks. -->
<TextBlock Style="{StaticResource TextBlockStyle}" Text=”Hello”/>
<TextBlock Style="{StaticResource TextBlockStyle}" Text=”World”/>
</StackPanel>
This example shows how to use multiple Setter
statements inside the VisualState.Setters property to apply discrete property value changes on various elements (without animations) when a VisualState is applied.
<Page>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="NarrowState">
<VisualState.Setters>
<Setter Target="myPanel.Orientation" Value="Vertical"/>
<Setter Target="myPanel.Width" Value="380"/>
<Setter Target="myTextBlock.MaxLines" Value="3"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<StackPanel x:Name="myPanel" Orientation="Horizontal">
<TextBlock x:Name="myTextBlock" MaxLines="5" Style="{ThemeResource BodyTextBlockStyle}"/>
</StackPanel>
</Grid>
</Page>
You can also apply setters to attached property values, by specifying the attached property name in the AttachedPropertyProvider.PropertyName form. For example, to use a Setter for the attached property Canvas.Left, use this XAML.
<Setter Property="Canvas.Left" Value="100"/>
To update a value of an attached property using Target
, place the attached property path inside parentheses. This example shows how to update the RelativePanel.AlignRightWithPanel
value on an element with the name 'TitleTextBlock'.
<RelativePanel>
<TextBlock x:Name="TitleTextBlock" Text="Title"/>
</RelativePanel>
...
<Setter Target="TitleTextBlock.(RelativePanel.AlignRightWithPanel)" Value="True"/>
Remarks
Use Setter
statements to set a property value within a Style or a VisualState.
The Setter.Target property can be used in either a Style or a VisualState, but in different ways. When used in a Style
, the property that needs to be modified can be specified directly. When used in VisualState
, the Target
property must be given a TargetPropertyPath (dotted syntax with a target element and property explicitly specified).
The Setter.Property property can be used only in a Style and not in a VisualState. Starting in Windows 10, you can use Setter.Target everywhere instead of Setter.Property
.
You must specify both the Value, and the Target or Property, on a Setter
. Otherwise, an exception is thrown (either a parse exception or runtime error, depending on whether the Setter is created in XAML or modified in code).
If you're accessing a Setter
instance using code, you cannot change the value of any property of a Setter
instance if the value of the IsSealed property on a parent Style is true
. This is also reported by the IsSealed property on an individual Setter
. The system sets these properties to true
when the runtime applies styles to UI elements and displays them in the UI. Attempting to change a sealed Setter
throws a runtime error.
Migration notes
- Windows Presentation Foundation (WPF) and Microsoft Silverlight supported the ability to use a Binding expression to supply the Value for a
Setter
in a Style. The Windows Runtime doesn't support aBinding
usage for Setter.Value (theBinding
won't evaluate and theSetter
has no effect, you won't get errors, but you won't get the desired result either). When you convert XAML styles from WPF or Microsoft Silverlight XAML, replace anyBinding
expression usages with strings or objects that set values, or refactor the values as shared {StaticResource} markup extension values rather thanBinding
-obtained values.
Constructors
Setter() |
Initializes a new instance of the Setter class with no initial Property or Value. |
Setter(DependencyProperty, Object) |
Initializes a new instance of the Setter class with initial Property and Value information. |
Properties
Dispatcher |
Always returns |
DispatcherQueue |
Gets the |
IsSealed |
Gets a value that indicates whether this object is in an immutable state. (Inherited from SetterBase) |
Property |
Gets or sets the property to apply the Value to. |
Target |
Gets or sets the path of a property on a target element to apply the Value to. |
Value |
Gets or sets the value to apply to the property that is specified by the Setter. |
Methods
ClearValue(DependencyProperty) |
Clears the local value of a dependency property. (Inherited from DependencyObject) |
GetAnimationBaseValue(DependencyProperty) |
Returns any base value established for a dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject) |
GetValue(DependencyProperty) |
Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject) |
ReadLocalValue(DependencyProperty) |
Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject) |
RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback) |
Registers a notification function for listening to changes to a specific DependencyProperty on this DependencyObject instance. (Inherited from DependencyObject) |
SetValue(DependencyProperty, Object) |
Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject) |
UnregisterPropertyChangedCallback(DependencyProperty, Int64) |
Cancels a change notification that was previously registered by calling RegisterPropertyChangedCallback. (Inherited from DependencyObject) |