TextBox SelectionStart始终为0,自定义样式

时间:2018-01-18 14:12:38

标签: c# wpf textbox wpf-style

我有一个带有自定义样式的TextBox输入字段:

  <TextBox  Style="{StaticResource SettingsTextBoxHint}" KeyDown="textBoxInput_KeyDown" PreviewKeyDown="textBoxInput_PreviewKeyDown"  Name="TextBoxInput" Text="{Binding TextBoxInput, Mode=TwoWay}" Tag="{lex:LocText TypeAMessageHere}" VerticalScrollBarVisibility="Auto"  TextWrapping="Wrap"  FontSize="14" HorizontalAlignment="Stretch" VerticalAlignment="Center" VerticalContentAlignment="Center" BorderThickness="0" Margin="0,0,33,0"/>

我添加了自定义样式:

<Style TargetType="{x:Type TextBox}" x:Key="SettingsTextBoxHint" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">

                <Border x:Name="Border"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="White"
                        Padding="1,2,5,2"
                        BorderBrush="{StaticResource PrimaryColor}"
                        ToolTip="{TemplateBinding ToolTip}">
                    <Grid>
                        <TextBox Text="{Binding Path=Text,
                                            RelativeSource={RelativeSource TemplatedParent}, 
                                            Mode=TwoWay,
                                            UpdateSourceTrigger=PropertyChanged}"
                                 x:Name="textSource" 
                                 Background="Transparent" 
                                 BorderBrush="Transparent"
                                 TextWrapping="{TemplateBinding TextWrapping}"
                                 Panel.ZIndex="2"
                                 FontSize="12">
                            <TextBox.Style>
                                <Style TargetType="{x:Type TextBox}">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="TextBox">

                                                <Border x:Name="Border"
                                                        Background="Transparent"
                                                        BorderBrush="Transparent"
                                                        CornerRadius="0">
                                                    <ScrollViewer x:Name="PART_ContentHost" VerticalAlignment="Center" />
                                                </Border>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>

                                </Style>
                            </TextBox.Style>
                        </TextBox>
                        <TextBox  Margin="0,3,0,0" BorderThickness="0" Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}" Panel.ZIndex="1">
                            <TextBox.Style>
                                <Style TargetType="{x:Type TextBox}">
                                    <Setter Property="FontFamily" Value="{StaticResource RobotoRegularFont}" />
                                    <Setter Property="Foreground" Value="Transparent"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
                                            <Setter Property="Foreground" Value="#cccccc"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBox.Style>
                        </TextBox>
                    </Grid>
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter TargetName="Border" Property="BorderBrush" Value="DarkGray"/>
                    </Trigger>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter TargetName="textSource" Property="FocusManager.FocusedElement" Value="{Binding ElementName=textSource}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

其中一点是使TextBox透明并添加提示功能,通过Tag设置哪个文本。

一切都按我想要的方式运作。我遇到的问题是我正在编写一个新功能,我需要在TextBox中使用光标的位置。但SelectionStart或CaretIndex总是返回0值。如果我删除我的样式,我会得到正确的值。

谁能告诉我我错过了什么?

1 个答案:

答案 0 :(得分:1)

问题是您在 TextBox内使用了TextBox 。用户与内部文本框进行交互,其文本,插入符号位置等与外部文本框无关。

让我为你清理你的风格:

<Style x:Key="SettingsTextBoxHint"
       TargetType="{x:Type TextBox}"
       BasedOn="{StaticResource {x:Type TextBox}}">
  <Setter Property="BorderBrush"
          Value="{StaticResource PrimaryColor}" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="TextBox">
        <Border x:Name="Border"
                BorderThickness="{TemplateBinding BorderThickness}"
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                Padding="{TemplateBinding Padding}">
          <Grid>
            <ScrollViewer x:Name="PART_ContentHost" VerticalAlignment="Center" />
            <TextBlock x:Name="Hint"
                       Margin="3,1"
                       Text="{TemplateBinding Tag}"
                       FontStyle="Italic"
                       Foreground="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"
                       Visibility="Hidden" />
          </Grid>
        </Border>
        <ControlTemplate.Triggers>
          <Trigger Property="IsEnabled" Value="False">
            <Setter TargetName="Border"
                    Property="Background"
                    Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
            <Setter Property="Foreground"
                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
          </Trigger>
          <Trigger Property="Text" Value="">
            <Setter TargetName="Hint" Property="Visibility" Value="Visible"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

要在TextBox中找到光标位置,您可以使用Mouse.GetPosition(your_text_box)。要在文本中获取插入符号位置,请使用CaretIndex属性。但请注意,CaretIndex *不* 是依赖项属性,因此它不会引发更改通知。因此,您无法绑定它并期望更新绑定目标。