【问题标题】:Rounded Corner TextBox in WPFWPF中的圆角文本框
【发布时间】:2017-02-23 15:39:28
【问题描述】:

我在网上搜索带有圆角的 TextBox 并找到如下的 xaml 代码:

 <Style TargetType="{x:Type my1:CustomTextBox}">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate >
                        <Border Background="{TemplateBinding Background}" x:Name="Bd" 
BorderThickness="2" CornerRadius="5" BorderBrush="#FFF9EAB6">
                            ***<ScrollViewer x:Name="PART_ContentHost" />***
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Bd" Property="BorderBrush" Value="#FFC7B0B0"/>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="True">
                                <Setter TargetName="Bd" Property="BorderBrush" Value="#FFC7B0B0"/>
                                <Setter Property="Foreground" Value="Black"/>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="False">
                                <Setter Property="Foreground" Value="#FFC7B0B0"/>
                            </Trigger>
                            <Trigger Property="Width" Value="Auto">
                                <Setter Property="MinWidth" Value="120"/>
                            </Trigger>
                            <Trigger Property="Height" Value="Auto">
                                <Setter Property="MinHeight" Value="27"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>

                </Setter.Value>
            </Setter>
        </Style>

我想知道是什么

<ScrollViewer x:Name="PART_ContentHost" />

如果从中删除这一行,为什么不能正确使用我的模板, 请详细告诉我。

非常感谢。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    名为“PART_ContentHost”的部分包含控件核心,这是文本框本身,除了装饰。后面的文本框代码会寻找它,所以如果你重命名删除,控件将无法工作。 在这种情况下,内容是可滚动的(因为文本框可以水平和垂直滚动文本)。

    【讨论】:

    【解决方案2】:

    使用 xaml deign 的这一部分:

     <TextBox x:Name="usernameText" Height="30" Width="300"  TextWrapping="Wrap" Text="" FontSize="20" HorizontalContentAlignment="Center" LostFocus="usernameText_LostFocus">
                            <TextBox.Resources>
                                <Style TargetType="{x:Type Border}">
                                    <Setter Property="CornerRadius" Value="10"/>
                                </Style>
                            </TextBox.Resources>
                        </TextBox>
    

    【讨论】:

      【解决方案3】:

      如果你需要一个简单的圆角文本框,你可以这样做:

      <Border Padding="5" CornerRadius="5" BorderThickness="1" BorderBrush="LightGray" SnapsToDevicePixels="True" Background="White">
          <TextBox Background="Transparent" BorderThickness="0">This is beautifull ;)</TextBox>
      </Border>
      

      【讨论】:

      • 是的,它可以工作,因为您将文本框背景颜色设置为透明,但我认为当您为 TextBox 设置不同的背景颜色并且此背景颜色与边框设置不同时,它不起作用。那么在这种情况下,你是如何面对这个问题的呢?
      【解决方案4】:

      ScrollViewer 包含控件的实际内容。您的控件不是真正的文本框,而是围绕 ScrollViewer 的边框(带有圆角),然后您需要在其中放置文本。如果不需要滚动,可以将 ScrollViewer 替换为文本框,即:

      改变

      <ScrollViewer x:Name="PART_ContentHost" />
      

      <TextBox x:Name="PART_ContentHost" />
      

      【讨论】:

      • 感谢您的回答,我根据上面的解释更改了我的代码,但还不能正常工作,
      • 你能把你的CustomTextBox课程的内容也贴出来吗?为了完全理解您的代码,两者都是必需的。
      猜你喜欢
      • 1970-01-01
      • 2021-12-29
      • 2017-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      相关资源
      最近更新 更多