【问题标题】:WPF TabStop TextBoxes not working on Win10WPF TabStop 文本框在 Win10 上不起作用
【发布时间】:2017-02-21 19:32:03
【问题描述】:

我正在努力测试我的 wpf 应用程序,其中我有一个带有 TextBoxes 的窗口,其中包含来自 Application.Resources 的自定义样式

该样式实现了一个占位符,并且因为它干扰了制表符(它关注的是占位符而不是 TB 内容)我不得不添加一些 IsTabStop 代码。

当我在 Win7 上调试和发布 exe 时,它​​工作得非常好,但选项卡在 Win10 上不起作用,它只是忽略了文本框,只是通过其他没有实现这种样式的控件的选项卡。

任何帮助都会很棒!

这里是样式代码:

<Style x:Key="placeHolderNoline" TargetType="{x:Type TextBox}" BasedOn="{StaticResource tb}">
            <Setter Property="IsTabStop" Value="False"/>

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">


                        <Grid>
                            <TextBox Text="{Binding Path=Text,
                                            RelativeSource={RelativeSource TemplatedParent}, 
                                            Mode=TwoWay,
                                            UpdateSourceTrigger=PropertyChanged}"
                             x:Name="textSource" 
                             Background="Transparent" 
                             Panel.ZIndex="2" 
                                 BorderThickness="0,0,0,0"/>


                            <TextBox Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}" Panel.ZIndex="1" BorderThickness="0" IsTabStop="False">

                                <TextBox.Style>
                                    <Style TargetType="{x:Type TextBox}">
                                        <Setter Property="Foreground" Value="Transparent"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
                                                <Setter Property="Foreground" Value="LightGray"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBox.Style>
                            </TextBox>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

编辑:tb 风格:

 <Style TargetType="TextBox" x:Key="tb">

            <Setter Property="Foreground" Value="WhiteSmoke"/>
            <Setter Property="Background" Value="#33FFFFFF"/>
            <Setter Property="BorderThickness" Value="0"/>
        </Style>

【问题讨论】:

    标签: wpf xaml windows-10 tabstop


    【解决方案1】:

    TextBox 中的ControlTemplate 不应包含另一个或两个其他TextBox 元素。

    试试这种风格:

    <Style x:Key="placeHolderNoline" TargetType="TextBox" BasedOn="{StaticResource tb}">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="BorderBrush" Value="#FFABAdB3"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <Grid>
                            <TextBlock Text="{TemplateBinding Tag}" Foreground="LightGray" Background="{TemplateBinding Background}">
                                <TextBlock.Style>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Setter Property="Visibility" Value="Collapsed"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource AncestorType=TextBox}}" Value="">
                                                <Setter Property="Visibility" Value="Visible"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                            <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/>
                        </Trigger>
                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter Property="BorderBrush" TargetName="border" Value="#FF569DE5"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    【讨论】:

    • 这在两个窗口上都有效,尽管以前的样式丢失了(占位符仍然有效)。给我一些时间检查代码
    • 好的,正在应用背景的BasedOn="{StaticResource tb}" 现在不工作了。有什么想法吗?
    • 请编辑您的问题并包含“tb”样式的内容。
    • 只是避免在 placeHolderNoline 样式中设置 Background 和 Foreground 属性。从样式中删除这些设置器。
    • 好的,成功了!谢谢你,亲爱的先生。现在我还发现代码隐藏生成的工具提示不会在 W10 上消失 -_- 看起来我会在调试时玩得更开心
    猜你喜欢
    • 2011-10-15
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多