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 中指定附加物件名稱。PropertyName 表格。 例如,若要使用 Setter 來管理附加屬性 Canvas.Left,請使用這個 XAML。

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

若要使用 ,將附加屬性 Target路徑置於括號內。 這個範例展示了如何更新 RelativePanel.AlignRightWithPanel 名為「TitleTextBlock」的元素的值。

<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

你必須同時指定 Value 以及 TargetPropertySetter 否則會拋出例外(可能是解析異常或執行時錯誤,視 Setter 是用 XAML 建立還是程式碼修改而定)。

如果你是用程式碼存取實Setter例,當父樣式上的 IsSealed 屬性值為 true時,你就不能更改實例任何屬性Setter的值。 這也由 Issealed 物業在個人 Setter身上報告。 系統會將這些屬性設定為 true 執行時對 UI 元素套用樣式並在 UI 中顯示。 嘗試更改封 Setter 存會跳出執行時錯誤。

建構函式

名稱 Description
Setter()

初始化一個沒有初始屬性的新 Setter 類別實例。

Setter(DependencyProperty, Object)

初始化一個新的 Setter 類別實例,並初始化 PropertyValue 資訊。

屬性

名稱 Description
Dispatcher

它總是在 Windows App SDK 應用程式中回傳 null 。 改用 DispatcherQueue

(繼承來源 DependencyObject)
DispatcherQueue

得到 DispatcherQueue 這個物件所關聯的那個。 代表 DispatcherQueue 一個功能,即使程式碼是由非 UI 執行緒發起,也能存取 UI DependencyObject 執行緒。

(繼承來源 DependencyObject)
IsSealed

會得到一個值,表示該物件是否處於不可變狀態。

(繼承來源 SetterBase)
Property

取得或設定該屬性以套用該

Target

取得或設定目標元素屬性的路徑,將值套用到該 上。

Value

取得或設定值以套用於 設定者指定的屬性。

方法

名稱 Description
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)

適用於

另請參閱