wpf 文本框文本修剪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10227014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:23:43  来源:igfitidea点击:

TextBox TextTrimming

wpfxaml

提问by Guillaume

I would like to apply a TextTrimmingoption on a TextBox(Not a TextBlock).

我想TextTrimmingTextBox(Not a TextBlock)上应用一个选项。

The compiler tells me that the TextTrimmingoptions is not a valid property of the Textbox.

编译器告诉我这些TextTrimming选项不是Textbox.

I could do a fancy control that is a Textblockand once it's clicked will become a Textboxand conversely go back to being a Textblockonce the focus is lost.

我可以做一个花哨的控件,Textblock一旦点击它就会变成 a Textbox,相反,Textblock一旦失去焦点,就会回到 a 。

Before going this way I would like to know if a built-in function already exists (or is there a smarter way) to allow you to do that?

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

EDIT: What I want to have in the end is a TextBoxwhich is trim (the full content will be display in a tooltip) but when the user select the TextBox(enter in "edit mode") the full content will be display (without trim) therefore the user will be able to modify the full text. when the TextBoxlost the focus (go back to "view mode") the content will be trim again.

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

Thanks

谢谢

回答by Dan Puzey

Try a style like this (I've added background colours to make the change obvious):

尝试这样的样式(我添加了背景颜色以使更改显而易见):

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

回答by Nicholas Miller

Dan Puzey has a great answer, but I wanted to add more so that the style of the TextBlockappeared likea TextBox.

丹Puzey有一个伟大的答案,但我想添加更多,这样的风格TextBlock似乎像一个TextBox

Here is the XAML style I came up with:

这是我想出的 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>

This is what the control looks like when it has no keyboard focus:

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

TextBlock - No Keyboard Focus

TextBlock - 没有键盘焦点

This is what the control looks like after gaining keyboard focus:

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

TextBox - With Keyboard Focus

文本框 - 带键盘焦点

回答by Akanksha Gaur

I think wat you are looking for is this

我想你要找的是这个

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

I hope it helps :)

我希望它有帮助:)

It will call the trim function and pass any arguments, if you want. You can also use split and pass the delimiters as arguments.

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

You can find more on Binding.Converter here

您可以在此处找到有关 Binding.Converter 的更多信息

回答by Nicolas Repiquet

You may create a control template for your TextBoxthat display the usual editor when focused, and a TextBlockwith trimming when it's not.

您可以为您创建一个控制模板,TextBox在聚焦时显示通常的编辑器,并TextBlock在没有时显示修剪。

回答by Mr.B

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

I would use different control template: template with trimming when text box is not focused, whereas text box is focused I would use the regular template to allow text selection. Needs to replace TextBox control template.

我将使用不同的控制模板:当文本框未聚焦时带有修剪的模板,而文本框聚焦时我将使用常规模板来允许文本选择。需要替换 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>