StrokeDashArray 不适用于 VisualStateManager (UWP)

StrokeDashArray not working with VisualStateManager (UWP)

如果文本框处于禁用状态,我希望它有一个虚线边距,我的代码如下所示

 <Page.Resources>
    <SolidColorBrush x:Key="TextBoxBackgroundThemeBrush" Color="#FFFFFFFF" />
    <SolidColorBrush x:Key="TextBoxBorderThemeBrush" Color="#FFFFFFFF" />
    <SolidColorBrush x:Key="TextBoxForegroundThemeBrush" Color="#FF000000" />
    <SolidColorBrush x:Key="TextBoxForegroundHeaderThemeBrush" Color="#FFFFFFFF" />
    <SolidColorBrush x:Key="TextBoxPlaceholderTextThemeBrush" Color="#AB000000" />
    <SolidColorBrush x:Key="TextSelectionHighlightColorThemeBrush" Color="#FF4617B4"/>
    <x:Double x:Key="TextControlThemeMinHeight">32</x:Double>
    <x:Double x:Key="TextControlThemeMinWidth">64</x:Double>
    <Thickness x:Key="TextControlBorderThemeThickness">2</Thickness>
    <Thickness x:Key="TextControlThemePadding">10,3,10,5</Thickness>
    <FontFamily x:Key="ContentControlThemeFontFamily">Segoe UI</FontFamily>
    <x:Double x:Key="ControlContentThemeFontSize">14.667</x:Double>
    <!-- Default style for Windows.UI.Xaml.Controls.TextBox -->
    <Style TargetType="TextBox" x:Key="TextBoxStyle1">
        <Setter Property="MinWidth" Value="{ThemeResource TextControlThemeMinWidth}" />
        <Setter Property="MinHeight" Value="{ThemeResource TextControlThemeMinHeight}" />
        <Setter Property="Foreground" Value="{ThemeResource TextBoxForegroundThemeBrush}" />
        <Setter Property="Background" Value="{ThemeResource TextBoxBackgroundThemeBrush}" />
        <Setter Property="BorderBrush" Value="{ThemeResource TextBoxBorderThemeBrush}" />
        <Setter Property="SelectionHighlightColor" Value="{ThemeResource TextSelectionHighlightColorThemeBrush}" />
        <Setter Property="BorderThickness" Value="{ThemeResource TextControlBorderThemeThickness}" />
        <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
        <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
        <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False" />
        <Setter Property="Padding" Value="{ThemeResource TextControlThemePadding}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TextBox">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderElement"
                                                                       Storyboard.TargetProperty="StrokeDashArray">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="2,2" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Border x:Name="BackgroundElement"
                        Grid.Row="1"
                        Background="{TemplateBinding Background}"
                        Margin="{TemplateBinding BorderThickness}"
                        Grid.ColumnSpan="2"
                        Grid.RowSpan="1"/>
                        <Line  x:Name="BorderElement" Stroke="Red" X2="10000"  Grid.Row="1" Grid.ColumnSpan="2" Grid.RowSpan="1" 
                               Margin="0 39 0 0"    StrokeThickness="2"  StrokeDashCap="Round" />

                        <ContentPresenter x:Name="HeaderContentPresenter"
                                  Grid.Row="0"
                                  Foreground="{ThemeResource TextBoxForegroundHeaderThemeBrush}"
                                  Margin="0,4,0,4"
                                  Grid.ColumnSpan="2"
                                  Content="{TemplateBinding Header}"
                                  ContentTemplate="{TemplateBinding HeaderTemplate}"
                                 />
                        <ScrollViewer x:Name="ContentElement"
                              Grid.Row="1"
                              HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
                              HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                              VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
                              VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                              IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}"
                              IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}"
                              IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
                              Margin="{TemplateBinding BorderThickness}"
                              Padding="{TemplateBinding Padding}"
                              IsTabStop="False"
                              AutomationProperties.AccessibilityView="Raw"
                              ZoomMode="Disabled" />
                        <ContentControl x:Name="PlaceholderTextContentPresenter"
                              Grid.Row="1"
                              Foreground="{ThemeResource TextBoxPlaceholderTextThemeBrush}"
                              Margin="{TemplateBinding BorderThickness}"
                              Padding="{TemplateBinding Padding}"
                              IsTabStop="False"
                              Grid.ColumnSpan="2"
                              Content="{TemplateBinding PlaceholderText}" 
                              IsHitTestVisible="False"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="100"></RowDefinition>
    </Grid.RowDefinitions>
    <TextBox Height="40" Width="300" x:Name="txtDisabled" PlaceholderText="I'm Disabled"  IsEnabled="False" Grid.Row="1" Style="{StaticResource TextBoxStyle1}" ></TextBox>
</Grid>

我觉得一切都很好,但这并没有生成虚线,而是给出了完整的法线。如您所见,我在 Disabled 状态下为 BorderElement 设置了 StrokeDashArray。如果我直接向该行 (BorderElement) 提供相同的 StrokeDashArray,它工作正常(但在我的情况下,我只希望虚线用于禁用状态)。我怎样才能做到这一点?

StrokeDashArray not working with VisualStateManager (UWP)

问题是您错过了 Normal VisualState,这将导致切换 TextBox IsEnabled 属性 时样式无法更改。请将 <VisualState x:Name="Normal" /> 添加到 VisualStateGroup

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="CommonStates">
        <VisualState x:Name="Normal" />
        <VisualState x:Name="Disabled">
            <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderElement" Storyboard.TargetProperty="StrokeDashArray">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="2,2" />
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>