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,但以不同方式使用。 在 中使用 Style 時,可以直接指定需要修改的屬性。 在 中使用 VisualState 時, Target 屬性必須指定 TargetPropertyPath (點語法,並明確指定目標元素和屬性) 。

Setter.Property屬性只能在Style中使用,而不能用於VisualState。 從 Windows 10 開始,您可以在任何地方使用Setter.Target,而不是 Setter.Property

您必須在 上 Setter 同時指定ValueTargetProperty。 否則,根據 Setter 是在 XAML 中建立或修改程式碼) 而定,在剖析例外狀況或執行時間錯誤 (擲回例外狀況。

如果您使用程式碼存取 Setter 實例,如果父Style上的IsSealed屬性值為 true ,則無法變更實例的任何屬性值 Setter 。 這是由個別 Setter 上的IsSealed屬性所報告。 當執行時間將樣式套用至 UI 元素並在 UI 中顯示樣式時,系統會將這些屬性 true 設定為 。 嘗試變更密封 Setter 擲回執行階段錯誤。

移轉注意事項

  • Windows Presentation Foundation (WPF) 和 Microsoft Silverlight 支援使用Binding運算式在Style中提供ValueSetter 的功能。 Windows 執行階段不支援 BindingSetter.Value的使用方式, (Binding 不會評估 ,而且 Setter 沒有作用,您不會收到錯誤,但不會取得所需的結果) 。 當您從 WPF 或 Microsoft Silverlight XAML 轉換 XAML 樣式時,請將任何 Binding 運算式使用方式取代為設定值的字串或物件,或將值重構為共用 {StaticResource} 標記延伸 值,而不是 Binding 取得的值。

建構函式

Setter()

初始化沒有初始PropertyValueSetter類別的新實例。

Setter(DependencyProperty, Object)

使用初始PropertyValue資訊,初始化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)

適用於

另請參閱