通过


Setter 类

定义

将值应用于 StyleVisualState 中的属性。

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 .../>
继承
Object Platform::Object IInspectable DependencyObject SetterBase Setter
属性

示例

此示例演示如何在 TextBlock 元素的样式中使用Setter语句。

<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>

此示例演示如何在 VisualState.Setters 属性中使用多个Setter语句在应用 VisualState 时对各种元素(没有动画)应用离散属性值更改。

<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>

还可以通过在 AttachedPropertyProvider 中指定附加属性名称,将 setter 应用于附加属性值。PropertyName 窗体。 例如,若要为附加属性 Canvas.Left 使用 Setter,请使用此 XAML。

<Setter Property="Canvas.Left" Value="100"/>

若要使用 Target 更新附加属性的值,请将附加属性路径置于括号内。 此示例演示如何使用名称“TitleTextBlock”更新 RelativePanel.AlignRightWithPanel 元素上的值。

<RelativePanel>
    <TextBlock x:Name="TitleTextBlock" Text="Title"/>
</RelativePanel>

...

<Setter Target="TitleTextBlock.(RelativePanel.AlignRightWithPanel)" Value="True"/>

注解

使用 Setter 语句在 StyleVisualState 中设置属性值。

Setter.Target 属性可以在 StyleVisualState 中使用,但以不同的方式使用。 在 a1 Style中使用时,可以直接指定需要修改的属性。 使用时 VisualStateTarget 必须为属性指定 TargetPropertyPath (具有显式指定目标元素和属性的点式语法)。

Setter.Property 属性只能在 Style 中使用,不能在 VisualState 中使用。 从 Windows 10 开始,你可以在任何地方使用 Setter.Target ,而不是 Setter.Property

必须在 . 上Setter同时指定 ValueTargetProperty。 否则,将引发异常(分析异常或运行时错误,具体取决于 Setter 是在 XAML 中创建还是在代码中修改)。

如果使用代码访问Setter实例,则如果父样式上的 IsSealed 属性的值为true,则无法更改实例的任何属性Setter的值。 此外, IsSealed 属性对个人 Setter也报告了这一点。 当运行时向 UI 元素应用样式并在 UI 中显示样式时,系统将设置这些属性 true 。 尝试更改密封 Setter 的会引发运行时错误。

构造函数

名称 说明
Setter()

初始化不带初始属性Setter 类的新实例。

Setter(DependencyProperty, Object)

使用初始属性信息初始化 Setter 类的新实例。

属性

名称 说明
Dispatcher

始终在 Windows 应用 SDK 应用中返回 null 。 请改用 DispatcherQueue

(继承自 DependencyObject)
DispatcherQueue

获取 DispatcherQueue 与此对象关联的对象。 表示 DispatcherQueue 即使代码是由非 UI 线程启动,也可以访问 DependencyObject UI 线程上的设施。

(继承自 DependencyObject)
IsSealed

获取一个值,该值指示此对象是否处于不可变状态。

(继承自 SetterBase)
Property

获取或设置要向其应用 Value 的属性。

Target

获取或设置要向其应用 Value 的目标元素的属性的路径。

Value

获取或设置要应用于 Setter 指定的属性的值。

方法

名称 说明
ClearValue(DependencyProperty)

清除依赖属性的本地值。

(继承自 DependencyObject)
GetAnimationBaseValue(DependencyProperty)

返回为依赖属性建立的任何基值,在动画未处于活动状态的情况下,该属性将适用。

(继承自 DependencyObject)
GetValue(DependencyProperty)

DependencyObject 返回依赖属性的当前有效值。

(继承自 DependencyObject)
ReadLocalValue(DependencyProperty)

如果设置了本地值,则返回依赖属性的本地值。

(继承自 DependencyObject)
RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback)

注册一个通知函数,用于侦听此 DependencyObject 实例上对特定 DependencyProperty 所做的更改。

(继承自 DependencyObject)
SetValue(DependencyProperty, Object)

设置 DependencyObject 上的依赖属性的本地值。

(继承自 DependencyObject)
UnregisterPropertyChangedCallback(DependencyProperty, Int64)

取消以前通过调用 RegisterPropertyChangedCallback 注册的更改通知。

(继承自 DependencyObject)

适用于

另请参阅