TextBox TextTrimming

时间:2012-04-19 11:26:33

标签: wpf xaml

我想在 TextTrimming (非TextBox)上应用TextBlock选项。

编译器告诉我TextTrimming选项不是Textbox的有效属性。

我可以做一个Textblock的花哨控件,一旦点击它将成为Textbox,反过来一旦焦点丢失,就会回到Textblock

在这种方式之前我想知道内置函数是否已经存在(或者是否有更聪明的方法)允许你这样做?

编辑:我最终想要的是TextBox修剪(完整内容将显示在工具提示中)但是当用户选择TextBox时(输入“编辑”)模式“)将显示完整内容(无修剪),因此用户将能够修改全文。当TextBox失去焦点(返回“查看模式”)时,内容将再次修剪。

由于

6 个答案:

答案 0 :(得分:32)

尝试这样的风格(我添加了背景颜色以使变化明显):

    <Style TargetType="TextBox">
      <Setter Property="Background" Value="Yellow" />
      <Style.Triggers>
        <DataTrigger Binding="{Binding IsKeyboardFocused, RelativeSource={RelativeSource Self}}" Value="false">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="TextBox">
                <TextBlock Text="{TemplateBinding Text}"  TextTrimming="CharacterEllipsis" Background="Red" />
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </DataTrigger>
      </Style.Triggers>
    </Style>

答案 1 :(得分:2)

Dan Puzey has a great answer,但我想添加更多内容,以便TextBlock 的样式显示为 a TextBox

这是我提出的XAML风格:

<Style TargetType="TextBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsKeyboardFocused, RelativeSource={RelativeSource Self}}" Value="False">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TextBox">
                        <Border BorderThickness="1" CornerRadius="1">
                            <Border.BorderBrush>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="#FFABADB3" Offset="0"/>
                                    <GradientStop Color="#FFABADB3" Offset="0.044"/>
                                    <GradientStop Color="#FFE2E3EA" Offset="0.060"/>
                                    <GradientStop Color="#FFE3E9EF" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.BorderBrush>
                            <TextBlock Padding="4,2,0,0" Text="{TemplateBinding Text}" TextTrimming="CharacterEllipsis"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

这是控件在没有键盘焦点时的样子:

TextBlock - No Keyboard Focus

这是获得键盘焦点后控件的样子:

TextBox - With Keyboard Focus

答案 2 :(得分:1)

我认为您正在寻找的是这个

<TextBox Text="{Binding Path=String, Converter={StaticResource StringConverter}, ConverterParameter=Trim:Argument:AnotherArgument}" /> 

我希望它有所帮助:)

如果需要,它将调用trim函数并传递任何参数。 您也可以使用split并将分隔符作为参数传递。

您可以在Binding.Converter here

上找到更多信息

答案 3 :(得分:0)

您可以为TextBox创建一个控件模板,该模板在焦点时显示常用编辑器,而TextBlock则在不显示时显示。

答案 4 :(得分:0)

试试这个:

<TextBox Text={Binding Text}> </text>


private string _text;
public string Text
{
   get { return _text;}
   set
   {
      _text=value.Trim();
      NotifyPropertyChanged("Text");
   }
}

答案 5 :(得分:0)

enter image description here

enter image description here

我会使用不同的控制模板:当文本框没有聚焦时修剪模板,而文本框聚焦我会使用常规模板来允许文本选择。 需要替换TextBox控件模板。

 <ControlTemplate TargetType="{x:Type TextBox}"
                 x:Key="ControlTemplateTextBoxNormal">
    <Border Background="{TemplateBinding Background}"
            BorderThickness="{TemplateBinding BorderThickness}"
            BorderBrush="{TemplateBinding BorderBrush}">
        <Grid>
            <Border x:Name="ErrorElement"
                    Visibility="Collapsed"
                    BorderThickness="1.25"
                    BorderBrush="{StaticResource BrushError}">
                <Grid>
                    <Polygon x:Name="toolTipCorner"
                             Panel.ZIndex="2"
                             Margin="-1"
                             Points="9,9 9,0 0,0"
                             Fill="{StaticResource BrushError}"
                             HorizontalAlignment="Right"
                             VerticalAlignment="Top">
                        <Polygon.ToolTip>
                            <ToolTip Style="{StaticResource ToolTipStyleError}"
                                     Content="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={RelativeSource TemplatedParent}}" />
                        </Polygon.ToolTip>
                    </Polygon>
                </Grid>
            </Border>
            <ScrollViewer x:Name="PART_ContentHost"
                          Padding="{TemplateBinding Padding}"
                          BorderThickness="0"
                          IsTabStop="False"
                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                          TextElement.Foreground="{TemplateBinding Foreground}" />
            <TextBlock Text="{Binding Path=(behaviors:TextBoxBehaviors.WatermarkText), RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type TextBox}}}"
                       IsHitTestVisible="False"
                       Visibility="Collapsed"
                       HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                       VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                       Margin="{TemplateBinding Padding}"
                       Foreground="Gray"
                       x:Name="watermark" />
        </Grid>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsFocused"
                 Value="False">
            <Setter Property="Visibility"
                    TargetName="watermark"
                    Value="Visible" />
        </Trigger>
        <Trigger Property="Validation.HasError"
                 Value="True">
            <Setter Property="Visibility"
                    TargetName="ErrorElement"
                    Value="Visible" />
        </Trigger>
        <!--<Trigger Property="behaviors:TextBoxBehaviors.WatermarkText"
                             Value="True">
                        <Setter Property="Visibility"
                                TargetName="ErrorElement"
                                Value="Visible" />
                    </Trigger>-->

    </ControlTemplate.Triggers>
</ControlTemplate>
 <Style TargetType="{x:Type TextBox}"
       BasedOn="{StaticResource {x:Type TextBox}}"
       x:Key="TextBoxStyleTrimming">
    <Setter Property="BorderThickness"
            Value="1" />
    <Setter Property="Validation.ErrorTemplate"
            Value="{x:Null}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border Background="{TemplateBinding Background}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        BorderBrush="{TemplateBinding BorderBrush}">
                    <Grid>
                        <Border x:Name="ErrorElement"
                                Visibility="Collapsed"
                                BorderThickness="1.25"
                                BorderBrush="{StaticResource BrushError}">
                            <Grid>
                                <Polygon x:Name="toolTipCorner"
                                         Panel.ZIndex="2"
                                         Margin="-1"
                                         Points="9,9 9,0 0,0"
                                         Fill="{StaticResource BrushError}"
                                         HorizontalAlignment="Right"
                                         VerticalAlignment="Top">
                                    <Polygon.ToolTip>
                                        <ToolTip Style="{StaticResource ToolTipStyleError}"
                                                 Content="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={RelativeSource TemplatedParent}}" />
                                    </Polygon.ToolTip>
                                </Polygon>
                            </Grid>
                        </Border>
                        <TextBlock Padding="{TemplateBinding Padding}"
                                   Text="{TemplateBinding Text}"
                                   TextTrimming="CharacterEllipsis"
                                   HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                   VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                   TextElement.Foreground="{TemplateBinding Foreground}" />
                        <TextBlock Text="{Binding Path=(behaviors:TextBoxBehaviors.WatermarkText), RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type TextBox}}}"
                                   IsHitTestVisible="False"
                                   Visibility="Collapsed"
                                   HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                   VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                   Margin="{TemplateBinding Padding}"
                                   Foreground="Gray"
                                   x:Name="watermark" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsFocused"
                             Value="False">
                        <Setter Property="Visibility"
                                TargetName="watermark"
                                Value="Visible" />
                    </Trigger>
                    <Trigger Property="Validation.HasError"
                             Value="True">
                        <Setter Property="Visibility"
                                TargetName="ErrorElement"
                                Value="Visible" />
                    </Trigger>

                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsKeyboardFocused"
                 Value="True">
            <Setter Property="Template"
                    Value="{StaticResource ControlTemplateTextBoxNormal}" />
        </Trigger>
    </Style.Triggers>
</Style>