Control.UseSystemFocusVisuals 屬性

定義

取得或設定值,這個值表示控制項是否使用由系統或控制項範本中定義的焦點視覺效果。

public:
 property bool UseSystemFocusVisuals { bool get(); void set(bool value); };
bool UseSystemFocusVisuals();

void UseSystemFocusVisuals(bool value);
public bool UseSystemFocusVisuals { get; set; }
var boolean = control.useSystemFocusVisuals;
control.useSystemFocusVisuals = boolean;
Public Property UseSystemFocusVisuals As Boolean

屬性值

Boolean

bool

如果控制項使用系統所繪製的焦點視覺效果,則為true;如果控制項使用ControlTemplate中定義的焦點視覺效果,則為false。 預設值為 false;請參閱。

範例

此範例顯示 ControlTemplate ,定義 Button的自訂焦點視覺效果。

控制項範本的某些元素不會顯示,以更清楚顯示相關元件。

<Style TargetType="Button">

<!-- Set UseSystemFocusVisuals to false. -->
    <Setter Property="UseSystemFocusVisuals" Value="False"/> 
     ...

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="PointerOver">
                             ...

                            </VisualState>
                        </VisualStateGroup>

<!-- Add VisualStateGroup for FocusStates. -->
                        <VisualStateGroup x:Name="FocusStates">
                            <VisualState x:Name="Focused">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="FocusVisualWhite"
                                                     Storyboard.TargetProperty="Opacity"
                                                     To="1"
                                                     Duration="0"/>
                                    <DoubleAnimation Storyboard.TargetName="FocusVisualBlack"
                                                     Storyboard.TargetProperty="Opacity"
                                                     To="1"
                                                     Duration="0"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unfocused"/>
                            <VisualState x:Name="PointerFocused"/>
                        </VisualStateGroup>

                    </VisualStateManager.VisualStateGroups>
                    <Border x:Name="Border"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Margin="3">
                        <ContentPresenter x:Name="ContentPresenter"
                                          Content="{TemplateBinding Content}"
                                          ContentTransitions="{TemplateBinding ContentTransitions}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                          Margin="{TemplateBinding Padding}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                                          AutomationProperties.AccessibilityView="Raw"/>
                    </Border>

<!-- Add elements for focus visuals. -->
                    <Rectangle x:Name="FocusVisualWhite"
                               IsHitTestVisible="False"
                               Stroke="{ThemeResource FocusVisualWhiteStrokeThemeBrush}"
                               StrokeEndLineCap="Square"
                               StrokeDashArray="1,1"
                               Opacity="0"
                               StrokeDashOffset="1.5"/>
                    <Rectangle x:Name="FocusVisualBlack"
                               IsHitTestVisible="False"
                               Stroke="{ThemeResource FocusVisualBlackStrokeThemeBrush}"
                               StrokeEndLineCap="Square"
                               StrokeDashArray="1,1"
                               Opacity="0"
                               StrokeDashOffset="0.5"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

備註

此屬性預設為 false ,因此自訂 的 ControlTemplate 會如預期般運作。 不過,所有 XAML 架構控制項都會在其ControlTemplate中將此屬性設定為true,並使用系統繪製的焦點視覺效果。

若要定義控制項的自訂焦點視覺效果,您需要提供自訂 ControlTemplate。 在 ControlTemplate中,執行下列動作:

  • 如果您要修改預設 ControlTemplate,請務必將 UseSystemFocusVisuals 屬性設定為 false ,以關閉系統焦點視覺效果。 當設定為 false時,會呼叫 VisualStateManager 中的焦點狀態。
  • 定義 的 FocusStatesVisualStateGroup
  • 在群組中 FocusStates ,定義 、 UnfocusedPointerFocusedFocusedVisualStates。
  • 定義焦點視覺效果。

適用於

另請參閱