动态将标签更改为文本框的最佳做法

本文关键字:最佳 文本 标签 动态 | 更新日期: 2023-09-27 17:57:09

我有一个显示用户名称的小 WinRT XAML 标签。现在,当有人点击"编辑"按钮时,我想将其更改为文本框(可能带有浅色动画)。

实现这一点的好方法是什么?

下一步是在某些情况下,标签在单击时显示为文本框。

有没有办法使用样式来做到这一点?

动态将标签更改为文本框的最佳做法

始终使用文本框,但在模板未聚焦时更改模板。为此,您可以在 Blend 中打开 XAML 文件,右键单击文本框并选择"编辑模板"/"编辑副本..."。然后,"状态"选项卡将显示为模板定义的视觉状态,您可以隐藏"正常"状态的背景和边框。

这是我刚刚准备的示例:

<Page
    x:Class="App103.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App103"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Page.Resources>
        <Style
            x:Key="FlatTextBoxStyle"
            TargetType="TextBox">
            <Setter
                Property="MinWidth"
                Value="{StaticResource TextControlThemeMinWidth}" />
            <Setter
                Property="MinHeight"
                Value="{StaticResource TextControlThemeMinHeight}" />
            <Setter
                Property="Foreground"
                Value="{StaticResource TextBoxForegroundThemeBrush}" />
            <Setter
                Property="Background"
                Value="{StaticResource TextBoxBackgroundThemeBrush}" />
            <Setter
                Property="BorderBrush"
                Value="{StaticResource TextBoxBorderThemeBrush}" />
            <Setter
                Property="BorderThickness"
                Value="{StaticResource TextControlBorderThemeThickness}" />
            <Setter
                Property="FontFamily"
                Value="{StaticResource ContentControlThemeFontFamily}" />
            <Setter
                Property="FontSize"
                Value="{StaticResource ControlContentThemeFontSize}" />
            <Setter
                Property="ScrollViewer.HorizontalScrollBarVisibility"
                Value="Hidden" />
            <Setter
                Property="ScrollViewer.VerticalScrollBarVisibility"
                Value="Hidden" />
            <Setter
                Property="ScrollViewer.IsDeferredScrollingEnabled"
                Value="False" />
            <Setter
                Property="Padding"
                Value="{StaticResource TextControlThemePadding}" />
            <Setter
                Property="Margin"
                Value="0,0,0,10" />
            <Setter
                Property="TextWrapping"
                Value="Wrap" />
            <Setter
                Property="VerticalAlignment"
                Value="Top" />
            <Setter
                Property="Template">
                <Setter.Value>
                    <ControlTemplate
                        TargetType="TextBox">
                        <Grid>
                            <Grid.Resources>
                                <Style
                                    x:Name="DeleteButtonStyle"
                                    TargetType="Button">
                                    <Setter
                                        Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate
                                                TargetType="Button">
                                                <Grid>
                                                    <VisualStateManager.VisualStateGroups>
                                                        <VisualStateGroup
                                                            x:Name="CommonStates">
                                                            <VisualState
                                                                x:Name="Normal" />
                                                            <VisualState
                                                                x:Name="PointerOver">
                                                                <Storyboard>
                                                                    <ObjectAnimationUsingKeyFrames
                                                                        Storyboard.TargetProperty="Background"
                                                                        Storyboard.TargetName="BackgroundElement">
                                                                        <DiscreteObjectKeyFrame
                                                                            KeyTime="0"
                                                                            Value="{StaticResource TextBoxButtonPointerOverBackgroundThemeBrush}" />
                                                                    </ObjectAnimationUsingKeyFrames>
                                                                    <ObjectAnimationUsingKeyFrames
                                                                        Storyboard.TargetProperty="BorderBrush"
                                                                        Storyboard.TargetName="BorderElement">
                                                                        <DiscreteObjectKeyFrame
                                                                            KeyTime="0"
                                                                            Value="{StaticResource TextBoxButtonPointerOverBorderThemeBrush}" />
                                                                    </ObjectAnimationUsingKeyFrames>
                                                                    <ObjectAnimationUsingKeyFrames
                                                                        Storyboard.TargetProperty="Foreground"
                                                                        Storyboard.TargetName="GlyphElement">
                                                                        <DiscreteObjectKeyFrame
                                                                            KeyTime="0"
                                                                            Value="{StaticResource TextBoxButtonPointerOverForegroundThemeBrush}" />
                                                                    </ObjectAnimationUsingKeyFrames>
                                                                </Storyboard>
                                                            </VisualState>
                                                            <VisualState
                                                                x:Name="Pressed">
                                                                <Storyboard>
                                                                    <ObjectAnimationUsingKeyFrames
                                                                        Storyboard.TargetProperty="Background"
                                                                        Storyboard.TargetName="BackgroundElement">
                                                                        <DiscreteObjectKeyFrame
                                                                            KeyTime="0"
                                                                            Value="{StaticResource TextBoxButtonPressedBackgroundThemeBrush}" />
                                                                    </ObjectAnimationUsingKeyFrames>
                                                                    <ObjectAnimationUsingKeyFrames
                                                                        Storyboard.TargetProperty="BorderBrush"
                                                                        Storyboard.TargetName="BorderElement">
                                                                        <DiscreteObjectKeyFrame
                                                                            KeyTime="0"
                                                                            Value="{StaticResource TextBoxButtonPressedBorderThemeBrush}" />
                                                                    </ObjectAnimationUsingKeyFrames>
                                                                    <ObjectAnimationUsingKeyFrames
                                                                        Storyboard.TargetProperty="Foreground"
                                                                        Storyboard.TargetName="GlyphElement">
                                                                        <DiscreteObjectKeyFrame
                                                                            KeyTime="0"
                                                                            Value="{StaticResource TextBoxButtonPressedForegroundThemeBrush}" />
                                                                    </ObjectAnimationUsingKeyFrames>
                                                                </Storyboard>
                                                            </VisualState>
                                                            <VisualState
                                                                x:Name="Disabled">
                                                                <Storyboard>
                                                                    <DoubleAnimation
                                                                        Duration="0"
                                                                        To="0"
                                                                        Storyboard.TargetProperty="Opacity"
                                                                        Storyboard.TargetName="BackgroundElement" />
                                                                    <DoubleAnimation
                                                                        Duration="0"
                                                                        To="0"
                                                                        Storyboard.TargetProperty="Opacity"
                                                                        Storyboard.TargetName="BorderElement" />
                                                                </Storyboard>
                                                            </VisualState>
                                                        </VisualStateGroup>
                                                    </VisualStateManager.VisualStateGroups>
                                                    <Border
                                                        x:Name="BorderElement"
                                                        BorderBrush="{StaticResource TextBoxButtonBorderThemeBrush}"
                                                        BorderThickness="{TemplateBinding BorderThickness}" />
                                                    <Border
                                                        x:Name="BackgroundElement"
                                                        Background="{StaticResource TextBoxButtonBackgroundThemeBrush}"
                                                        Margin="{TemplateBinding BorderThickness}">
                                                        <TextBlock
                                                            x:Name="GlyphElement"
                                                            Foreground="{StaticResource TextBoxButtonForegroundThemeBrush}"
                                                            FontFamily="{StaticResource SymbolThemeFontFamily}"
                                                            HorizontalAlignment="Center"
                                                            Text="&#xE0A4;"
                                                            VerticalAlignment="Center" />
                                                    </Border>
                                                </Grid>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </Grid.Resources>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition
                                    Width="*" />
                                <ColumnDefinition
                                    Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup
                                    x:Name="CommonStates">
                                    <VisualState
                                        x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames
                                                Storyboard.TargetProperty="Background"
                                                Storyboard.TargetName="BackgroundElement">
                                                <DiscreteObjectKeyFrame
                                                    KeyTime="0"
                                                    Value="{StaticResource TextBoxDisabledBackgroundThemeBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames
                                                Storyboard.TargetProperty="BorderBrush"
                                                Storyboard.TargetName="BorderElement">
                                                <DiscreteObjectKeyFrame
                                                    KeyTime="0"
                                                    Value="{StaticResource TextBoxDisabledBorderThemeBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames
                                                Storyboard.TargetProperty="Foreground"
                                                Storyboard.TargetName="ContentElement">
                                                <DiscreteObjectKeyFrame
                                                    KeyTime="0"
                                                    Value="{StaticResource TextBoxDisabledForegroundThemeBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <DoubleAnimation
                                                Duration="0"
                                                To="0"
                                                Storyboard.TargetProperty="(UIElement.Opacity)"
                                                Storyboard.TargetName="BorderElement"
                                                d:IsOptimized="True" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState
                                        x:Name="Normal">
                                        <Storyboard>
                                            <DoubleAnimation
                                                Duration="0"
                                                To="0"
                                                Storyboard.TargetProperty="Opacity"
                                                Storyboard.TargetName="BackgroundElement" />
                                            <DoubleAnimation
                                                Duration="0"
                                                To="0"
                                                Storyboard.TargetProperty="Opacity"
                                                Storyboard.TargetName="BorderElement" />
                                            <ObjectAnimationUsingKeyFrames
                                                Storyboard.TargetProperty="(Control.Foreground)"
                                                Storyboard.TargetName="ContentElement">
                                                <DiscreteObjectKeyFrame
                                                    KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <SolidColorBrush
                                                            Color="White" />
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState
                                        x:Name="PointerOver">
                                        <Storyboard>
                                            <DoubleAnimation
                                                Duration="0"
                                                To="0.095"
                                                Storyboard.TargetProperty="Opacity"
                                                Storyboard.TargetName="BackgroundElement" />
                                            <DoubleAnimation
                                                Duration="0"
                                                To="0"
                                                Storyboard.TargetProperty="Opacity"
                                                Storyboard.TargetName="BorderElement" />
                                            <ObjectAnimationUsingKeyFrames
                                                Storyboard.TargetProperty="(Control.Foreground)"
                                                Storyboard.TargetName="ContentElement">
                                                <DiscreteObjectKeyFrame
                                                    KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <SolidColorBrush
                                                            Color="White" />
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState
                                        x:Name="Focused">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames
                                                Storyboard.TargetProperty="(Control.Foreground)"
                                                Storyboard.TargetName="ContentElement">
                                                <DiscreteObjectKeyFrame
                                                    KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <SolidColorBrush
                                                            Color="Black" />
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup
                                    x:Name="ButtonStates">
                                    <VisualState
                                        x:Name="ButtonVisible">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames
                                                Storyboard.TargetProperty="Visibility"
                                                Storyboard.TargetName="DeleteButton">
                                                <DiscreteObjectKeyFrame
                                                    KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Visible</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState
                                        x:Name="ButtonCollapsed" />
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border
                                x:Name="BackgroundElement"
                                Background="{TemplateBinding Background}"
                                Grid.ColumnSpan="2"
                                Margin="{TemplateBinding BorderThickness}" />
                            <Border
                                x:Name="BorderElement"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Grid.ColumnSpan="2" />
                            <ScrollViewer
                                x:Name="ContentElement"
                                HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
                                HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                                IsTabStop="False"
                                IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}"
                                IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}"
                                IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
                                Margin="{TemplateBinding BorderThickness}"
                                Padding="{TemplateBinding Padding}"
                                VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                                VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
                                ZoomMode="Disabled" />
                            <Button
                                x:Name="DeleteButton"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Grid.Column="1"
                                FontSize="{TemplateBinding FontSize}"
                                IsTabStop="False"
                                Style="{StaticResource DeleteButtonStyle}"
                                Visibility="Collapsed"
                                VerticalAlignment="Stretch" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style
            x:Key="LabelTextBlockStyle"
            TargetType="TextBlock"
            BasedOn="{StaticResource BaselineTextStyle}">
            <Setter
                Property="FontWeight"
                Value="Normal" />
            <Setter
                Property="VerticalAlignment"
                Value="Top" />
            <Setter
                Property="HorizontalAlignment"
                Value="Right" />
            <Setter
                Property="Margin"
                Value="10,1,10,0" />
            <Setter
                Property="TextWrapping"
                Value="Wrap" />
        </Style>
    </Page.Resources>
    <Grid
        Background="{StaticResource ApplicationPageBackgroundThemeBrush}"
        Margin="0,1,0,0">
        <Grid.RowDefinitions>
            <RowDefinition
                Height="Auto" />
            <RowDefinition
                Height="Auto" />
            <RowDefinition
                Height="Auto" />
            <RowDefinition
                Height="Auto" />
            <RowDefinition
                Height="Auto" />
            <RowDefinition
                Height="Auto" />
            <RowDefinition
                Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition
                Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <TextBlock
            Text="Regular TextBox"
            Style="{StaticResource LabelTextBlockStyle}" />
        <TextBox
            Text="Value 1"
            Grid.Column="1"
            Margin="0,0,0,10" />
        <TextBlock
            Grid.Row="1"
            Text="Regular Disabled TextBox"
            Style="{StaticResource LabelTextBlockStyle}" />
        <TextBox
            Grid.Row="1"
            Text="Value 2"
            Grid.Column="1"
            Margin="0,0,0,10"
            IsEnabled="False" />
        <TextBlock
            Grid.Row="2"
            Text="Styled TextBox"
            Style="{StaticResource LabelTextBlockStyle}" />
        <TextBox
            Grid.Row="2"
            Text="Value 3"
            Style="{StaticResource FlatTextBoxStyle}"
            Grid.Column="1" />
        <TextBlock
            Grid.Row="3"
            Text="Styled Disabled TextBox"
            Style="{StaticResource LabelTextBlockStyle}" />
        <TextBox
            Grid.Row="3"
            Text="Value 4"
            Style="{StaticResource FlatTextBoxStyle}"
            Grid.Column="1"
            IsEnabled="False" />
        <TextBlock
            Grid.Row="4"
            Text="Styled TextBox"
            Style="{StaticResource LabelTextBlockStyle}" />
        <TextBox
            Grid.Row="4"
            Text="Value 5"
            Style="{StaticResource FlatTextBoxStyle}"
            Grid.Column="1" />
        <TextBlock
            Grid.Row="5"
            Text="Styled TextBox"
            Style="{StaticResource LabelTextBlockStyle}" />
        <TextBox
            Grid.Row="5"
            Text="Value 6"
            Style="{StaticResource FlatTextBoxStyle}"
            Grid.Column="1" />
        <TextBlock
            Grid.Row="6"
            Text="Styled TextBox"
            Style="{StaticResource LabelTextBlockStyle}" />
        <TextBox
            Grid.Row="6"
            Text="Value 7"
            Style="{StaticResource FlatTextBoxStyle}"
            Grid.Column="1" />
    </Grid>
</Page>