1

I am struggling with testing of my wpf application, in which I have a window with TextBoxes, that contain a custom style from Application.Resources

The style implements a placeholder, and because it interfered with tabbing(it focused the placeholder instead of TB content) I had to add some IsTabStop code.

It's working perfectly fine when Im debugging and also the release exe on Win7, but the tabbing doesn't work on Win10, it simply ignores TextBoxes and just tabs trough other controls that don't have this style implemented.

Any help would be great!

Here is the style code:

<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>

Edit: tb style:

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

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

1 Answers1

1

A ControlTemplate of a TextBox is not supposed to include another or two other TextBox elements.

Try this style:

<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>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • This worked on both windows, although the previous style is lost(the placeholder still works tho). Give me some minutes to check the code – someone1 Feb 21 '17 at 11:22
  • Okay, the `BasedOn="{StaticResource tb}"` which is applying background, is not working now. Any ideas? – someone1 Feb 21 '17 at 11:29
  • Please edit your question and include the contents of the "tb" style. – mm8 Feb 21 '17 at 11:30
  • 1
    Just avoid setting the Background and Foreground properties in the placeHolderNoline style. Remove these setters from the style. – mm8 Feb 21 '17 at 11:52
  • ALlright, it worked! Thank you, dear sir. Now I also found out that code-behind generated tooltips don't disappear on W10 -_- Looks like I'll have some more fun time with debugging – someone1 Feb 21 '17 at 12:20
  • Well, you could always ask a new question if you run into another issue :) – mm8 Feb 21 '17 at 12:27
  • Figured it out :) Gotta love Stackoverflow – someone1 Feb 21 '17 at 12:33