【问题标题】:WPF custom textbox tab index focusingWPF自定义文本框选项卡索引聚焦
【发布时间】:2013-12-21 17:59:04
【问题描述】:

我有这种风格的文本框。当我使用它并尝试使用 Tab 按钮文本框循环浏览我的内容时,使用这种样式的文本框在两次点击 Tab 按钮后获得焦点。不过,在第一个 Tab 命中时,“Focused”状态动画有效,但插入符号不存在。我再次点击标签,出现插入符号。

<Style x:Key="MPTextBox" TargetType="{x:Type TextBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border x:Name="border" BorderBrush="#FFEF7B54" BorderThickness="2" Background="White" CornerRadius="5">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="Disabled"/>
                                    <VisualState x:Name="ReadOnly"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="border">
                                                <EasingColorKeyFrame KeyTime="0:0:0.5" Value="#FF57C0AF"/>
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="FocusStates">
                                    <VisualState x:Name="Unfocused"/>
                                    <VisualState x:Name="Focused">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border">
                                                <EasingColorKeyFrame KeyTime="0" Value="#FFED4B15"/>
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <TextBox Background="{x:Null}" BorderBrush="{x:Null}" CaretBrush="#FFF05A29" BorderThickness="0" Margin="5,5,5,0" FontFamily="Public Enemy NF" FontSize="16" Foreground="#FFF05A29" HorizontalAlignment="Stretch" d:LayoutOverrides="Width, Height" VerticalAlignment="Stretch" Text="{Binding Text, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

我希望这一切只发生在一次 Tab 命中。

此文本框的另一件事是,如果我尝试访问事件处理程序中的 Text 属性,例如 KeyDown 事件,它会显示为 null,并且在文本框失去焦点之前永远不会设置控件中的文本。

【问题讨论】:

    标签: c# wpf textbox focus tabindex


    【解决方案1】:

    这里的问题是控件模板中存在文本框

      1234563将要更新的文本字符化为外部文本,然后在控件中指定代码 UpdateSourceTrigger=PropertyChanged 模板的文本框。 默认情况下,仅在失去焦点时,控件模板文本框中的值会在外部 TextBox(应用样式)中更新。
    1. 关于双焦点,是因为Border,然后是Textbox。

    但下面是为文本框定义 ControlTemplate 的正确方法 所以在setter中定义caretBrush,Border Brush如下

    <Style x:Key="MPTextBox" TargetType="{x:Type TextBox}">
    
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Microsoft_Windows_Themes:ClassicBorderDecorator x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" BorderStyle="Sunken" Background="{TemplateBinding Background}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="Disabled"/>
                                    <VisualState x:Name="ReadOnly"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="border">
                                                <EasingColorKeyFrame KeyTime="0:0:0.5" Value="#FF57C0AF"/>
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="FocusStates">
                                    <VisualState x:Name="Unfocused"/>
                                    <VisualState x:Name="Focused">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border">
                                                <EasingColorKeyFrame KeyTime="0" Value="#FFED4B15"/>
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <ScrollViewer x:Name="border" FontWeight="Bold">
                                <ScrollViewer.Background>
                                    <!-- Button: common to all buttons -->
                                    <SolidColorBrush Color="{DynamicResource VeryLightGray}"/>
                                </ScrollViewer.Background>
                            </ScrollViewer>                            
                        </Microsoft_Windows_Themes:ClassicBorderDecorator>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Background" Value="{x:Null}"/>
            <Setter Property="CaretBrush" Value="{DynamicResource {x:static SystemColors.WindowTextBrushKey}}"/>
    

    这里是 xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Classic"

    【讨论】:

    • 这还能给我定制的外观吗?
    • 是的,您的定制外观将被保留。文本框的样式可以通过设置器应用。文本框有一个边框,因此再次通过设置器设置边框样式“ "
    • Statis 来自 windows 。抱歉,将 's' 替换为 'S'。 xmlns:x="schemas.microsoft.com/winfx/2006/xaml" 并使用 x:Static。
    • 自定义外观取决于适当的设置器。我在我的代码中只提到了两个 setter 作为示例。但是您是否尝试过第 1 点。它是否有效?(对于每个字符更改,您是否能够看到外部文本框的文本正在更新)
    • 很抱歉,我会再试一次并回复:D 谢谢你的照顾,你真棒
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多