删除 Silverlight TextBox 鼠标悬停边框的最简单方法是什么?

发布于 2024-09-02 19:17:13 字数 1157 浏览 4 评论 0原文

我想在 Silverlight 应用程序中显示文本,以便用户可以将其复制并粘贴到其他地方(就像人们习惯在 HTML 上所做的那样)网站)。

如果我使用 TextBlock,则用户无法复制和粘贴。

因此我使用TextBox,但它有一个默认边框。我可以使用 BorderThickness="0" 删除边框,如下所示:

<TextBox
    Grid.Column="1"
    BorderThickness="0"
    Text="{Binding ViewModelBindingStringsBlockHelp}"/>

效果很好:

替代文本 http://www.deviantsart.com/upload/45p34i.png

但是,当用户将鼠标悬停在文本框上进行选择时文本,出现另一个边框

替代文本 http://www. deviantsart.com/upload/1k7m44p.png

我发现据称解决方案用于删除此边框,但令人难以置信的是,它们似乎需要 XAML 的页面

我正在寻找一个简单的解决方案,如下所示:

HoverBorderThickness="0"

什么是隐藏 Silverlight 文本框上的悬停边框的简单方法?

I want to display text in a Silverlight application so that the user can copy and paste it elsewhere (as one is used to doing on an HTML web site).

If I use TextBlock, then the user cannot copy and paste.

Therefore I use TextBox, but it has a default border on it. I can remove the border with BorderThickness="0" like this:

<TextBox
    Grid.Column="1"
    BorderThickness="0"
    Text="{Binding ViewModelBindingStringsBlockHelp}"/>

which works great:

alt text http://www.deviantsart.com/upload/45p34i.png

However, when the user hovers over the text box to select the text, another border appears:

alt text http://www.deviantsart.com/upload/1k7m44p.png

I've found purported solutions for removing this border but they unbelievably seem to require pages of XAML.

I'm looking for a simple solution like this:

HoverBorderThickness="0"

What is an easy way to hide the hover border on a Silverlight TextBox?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

慵挽 2024-09-09 19:17:13

不幸的是,你曾经遇到过想要做一些看似相对简单的事情,但你必须克服一些困难才能做到这一点。正如前面的答案中提到的,您需要按原样重新设计/重新模板化文本框。

至于悬停状态 - 这是 Silverlight 中您应该掌握的一个相对重要的点 - 每个控件都有单独的状态。

Expression Blend 是处理这些状态的绝佳方法 - 您可以轻松查看内置 XAML 模板和样式,并以最小的努力即时编辑它们。有时可能会很棘手,而且它肯定会使您的 XAML 有点庞大,但这就是让控件在您希望它做的时候准确地做您希望它做的事情的成本。

国家的力量——尤其是在 Blend 中——我保证你会意识到你的生活可以变得多么轻松。

Unfortunately you've encountered wanting to do something that compositely seems relatively simple, but you have to jump through a few hoops to do it. As forementioned in the previous answers, you will need to restyle / retemplate the textbox as it is.

As for the hover state - this is a relatively large point of Silverlight that you should get to grips with - it's the idea that every control has separate states.

Expression Blend is an excellent way to deal with these states - you can easily see what the in-build XAML templates and styles are and edit them on-the-fly with minimal effort. Can be tricky at times, and it sure-as-hell makes your XAML a little bulky, but that's the cost you have for making a control do exactly what you want it to do, when you want it to.

Harness the power of states - especially within Blend - and I guarantee you'll realise how much easier your life can be made.

嘿咻 2024-09-09 19:17:13

这是我创建没有背景和边框的只读文本框的解决方案:

public class MyTextBox : TextBox
{
    private String readOnlyText;
    public String ReadOnlyText
    {
        get
        {
            return readOnlyText;
        }
        set
        {
            readOnlyText = value;
            this.Text = readOnlyText;
        }
    }

    public MyTextBox()
    {
        BorderThickness = new Thickness(0);
        Background = new SolidColorBrush(Color.FromArgb(0, 255, 255, 255));
        BorderBrush = new SolidColorBrush(Color.FromArgb(0, 255, 255, 255));
        this.TextWrapping = System.Windows.TextWrapping.Wrap;
        this.GotFocus += new RoutedEventHandler(MyTextBox_GotFocus);
        this.MouseEnter += new MouseEventHandler(MyTextBox_MouseEnter);
        this.MouseMove += new MouseEventHandler(MyTextBox_MouseMove);
        this.TextChanged += new TextChangedEventHandler(MyTextBox_TextChanged);
    }

    void MyTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (!string.IsNullOrEmpty(this.ReadOnlyText))
        {
            var lS = this.SelectionStart; var lL = this.SelectionLength;
            this.Text = this.ReadOnlyText;
            this.SelectionStart = lS; this.SelectionLength = lL;
        }
    }

    void MyTextBox_MouseMove(object sender, MouseEventArgs e)
    {
        VisualStateManager.GoToState(this, "Normal", false);
    }

    void MyTextBox_MouseEnter(object sender, MouseEventArgs e)
    {
        VisualStateManager.GoToState(this, "Normal", false);
    }

    void MyTextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        VisualStateManager.GoToState(this, "Normal", false);
    }
}

This is my solution for creating readonly textbox without background and borders:

public class MyTextBox : TextBox
{
    private String readOnlyText;
    public String ReadOnlyText
    {
        get
        {
            return readOnlyText;
        }
        set
        {
            readOnlyText = value;
            this.Text = readOnlyText;
        }
    }

    public MyTextBox()
    {
        BorderThickness = new Thickness(0);
        Background = new SolidColorBrush(Color.FromArgb(0, 255, 255, 255));
        BorderBrush = new SolidColorBrush(Color.FromArgb(0, 255, 255, 255));
        this.TextWrapping = System.Windows.TextWrapping.Wrap;
        this.GotFocus += new RoutedEventHandler(MyTextBox_GotFocus);
        this.MouseEnter += new MouseEventHandler(MyTextBox_MouseEnter);
        this.MouseMove += new MouseEventHandler(MyTextBox_MouseMove);
        this.TextChanged += new TextChangedEventHandler(MyTextBox_TextChanged);
    }

    void MyTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (!string.IsNullOrEmpty(this.ReadOnlyText))
        {
            var lS = this.SelectionStart; var lL = this.SelectionLength;
            this.Text = this.ReadOnlyText;
            this.SelectionStart = lS; this.SelectionLength = lL;
        }
    }

    void MyTextBox_MouseMove(object sender, MouseEventArgs e)
    {
        VisualStateManager.GoToState(this, "Normal", false);
    }

    void MyTextBox_MouseEnter(object sender, MouseEventArgs e)
    {
        VisualStateManager.GoToState(this, "Normal", false);
    }

    void MyTextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        VisualStateManager.GoToState(this, "Normal", false);
    }
}
撧情箌佬 2024-09-09 19:17:13

不幸的是,您确实需要重新模板。使用 Blend 将比手动编辑更容易。

Unfortunately you do need to re-template. Using Blend will make this easier than hand-editing.

£烟消云散 2024-09-09 19:17:13

只需将此样式应用到您的文本框即可。这是您可以做到的唯一方法。

<Style x:Key="TextBoxStyle1" TargetType="TextBox">
   <Setter Property="BorderThickness" Value="1"/>
   <Setter Property="Background" Value="#FFFFFFFF"/>
   <Setter Property="Foreground" Value="#FF000000"/>
   <Setter Property="Padding" Value="2"/>
   <Setter Property="BorderBrush">
    <Setter.Value>
     <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
      <GradientStop Color="#FFA3AEB9" Offset="0"/>
      <GradientStop Color="#FF8399A9" Offset="0.375"/>
      <GradientStop Color="#FF718597" Offset="0.375"/>
      <GradientStop Color="#FF617584" Offset="1"/>
     </LinearGradientBrush>
    </Setter.Value>
   </Setter>
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="TextBox">
      <Grid x:Name="RootElement">
       <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CommonStates">
         <VisualState x:Name="Normal"/>
         <VisualState x:Name="MouseOver">

         </VisualState>
         <VisualState x:Name="Disabled">
          <Storyboard>
           <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="DisabledVisualElement"/>
          </Storyboard>
         </VisualState>
         <VisualState x:Name="ReadOnly">
          <Storyboard>
           <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ReadOnlyVisualElement"/>
          </Storyboard>
         </VisualState>
        </VisualStateGroup>
        <VisualStateGroup x:Name="FocusStates">
         <VisualState x:Name="Focused">          
         </VisualState>
         <VisualState x:Name="Unfocused">
         </VisualState>
        </VisualStateGroup>
        <VisualStateGroup x:Name="ValidationStates">
         <VisualState x:Name="Valid"/>
         <VisualState x:Name="InvalidUnfocused">
          <Storyboard>
           <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
            <DiscreteObjectKeyFrame KeyTime="0">
             <DiscreteObjectKeyFrame.Value>
              <Visibility>Visible</Visibility>
             </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
           </ObjectAnimationUsingKeyFrames>
          </Storyboard>
         </VisualState>
         <VisualState x:Name="InvalidFocused">
          <Storyboard>
           <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
            <DiscreteObjectKeyFrame KeyTime="0">
             <DiscreteObjectKeyFrame.Value>
              <Visibility>Visible</Visibility>
             </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
           </ObjectAnimationUsingKeyFrames>
           <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" Storyboard.TargetName="validationTooltip">
            <DiscreteObjectKeyFrame KeyTime="0">
             <DiscreteObjectKeyFrame.Value>
              <System:Boolean>True</System:Boolean>
             </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
           </ObjectAnimationUsingKeyFrames>
          </Storyboard>
         </VisualState>
        </VisualStateGroup>
       </VisualStateManager.VisualStateGroups>

        <Grid>
         <Border x:Name="ReadOnlyVisualElement" Background="#5EC9C9C9" Opacity="0"/>
         <Border x:Name="MouseOverBorder" BorderBrush="Transparent" BorderThickness="1">
          <ScrollViewer x:Name="ContentElement" BorderThickness="0" IsTabStop="False" Padding="{TemplateBinding Padding}"/>
         </Border>
        </Grid>

       <Border x:Name="DisabledVisualElement" BorderBrush="#A5F7F7F7" BorderThickness="{TemplateBinding BorderThickness}" Background="#A5F7F7F7" IsHitTestVisible="False" Opacity="0"/>       
       <Border x:Name="ValidationErrorElement" BorderBrush="#FFDB000C" BorderThickness="1" CornerRadius="1" Visibility="Collapsed">
        <ToolTipService.ToolTip>
         <ToolTip x:Name="validationTooltip" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" >
          <ToolTip.Triggers>
           <EventTrigger RoutedEvent="Canvas.Loaded">
            <BeginStoryboard>
             <Storyboard>
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsHitTestVisible" Storyboard.TargetName="validationTooltip">
               <DiscreteObjectKeyFrame KeyTime="0">
                <DiscreteObjectKeyFrame.Value>
                 <System:Boolean>true</System:Boolean>
                </DiscreteObjectKeyFrame.Value>
               </DiscreteObjectKeyFrame>
              </ObjectAnimationUsingKeyFrames>
             </Storyboard>
            </BeginStoryboard>
           </EventTrigger>
          </ToolTip.Triggers>
         </ToolTip>
        </ToolTipService.ToolTip>
        <Grid Background="Transparent" HorizontalAlignment="Right" Height="12" Margin="1,-4,-4,0" VerticalAlignment="Top" Width="12">
         <Path Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z" Fill="#FFDC000C" Margin="1,3,0,0"/>
         <Path Data="M 0,0 L2,0 L 8,6 L8,8" Fill="#ffffff" Margin="1,3,0,0"/>
        </Grid>
       </Border>
      </Grid>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
  </Style>

Just apply this style to your textbox. This is the only way you can do it.

<Style x:Key="TextBoxStyle1" TargetType="TextBox">
   <Setter Property="BorderThickness" Value="1"/>
   <Setter Property="Background" Value="#FFFFFFFF"/>
   <Setter Property="Foreground" Value="#FF000000"/>
   <Setter Property="Padding" Value="2"/>
   <Setter Property="BorderBrush">
    <Setter.Value>
     <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
      <GradientStop Color="#FFA3AEB9" Offset="0"/>
      <GradientStop Color="#FF8399A9" Offset="0.375"/>
      <GradientStop Color="#FF718597" Offset="0.375"/>
      <GradientStop Color="#FF617584" Offset="1"/>
     </LinearGradientBrush>
    </Setter.Value>
   </Setter>
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="TextBox">
      <Grid x:Name="RootElement">
       <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CommonStates">
         <VisualState x:Name="Normal"/>
         <VisualState x:Name="MouseOver">

         </VisualState>
         <VisualState x:Name="Disabled">
          <Storyboard>
           <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="DisabledVisualElement"/>
          </Storyboard>
         </VisualState>
         <VisualState x:Name="ReadOnly">
          <Storyboard>
           <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ReadOnlyVisualElement"/>
          </Storyboard>
         </VisualState>
        </VisualStateGroup>
        <VisualStateGroup x:Name="FocusStates">
         <VisualState x:Name="Focused">          
         </VisualState>
         <VisualState x:Name="Unfocused">
         </VisualState>
        </VisualStateGroup>
        <VisualStateGroup x:Name="ValidationStates">
         <VisualState x:Name="Valid"/>
         <VisualState x:Name="InvalidUnfocused">
          <Storyboard>
           <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
            <DiscreteObjectKeyFrame KeyTime="0">
             <DiscreteObjectKeyFrame.Value>
              <Visibility>Visible</Visibility>
             </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
           </ObjectAnimationUsingKeyFrames>
          </Storyboard>
         </VisualState>
         <VisualState x:Name="InvalidFocused">
          <Storyboard>
           <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
            <DiscreteObjectKeyFrame KeyTime="0">
             <DiscreteObjectKeyFrame.Value>
              <Visibility>Visible</Visibility>
             </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
           </ObjectAnimationUsingKeyFrames>
           <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" Storyboard.TargetName="validationTooltip">
            <DiscreteObjectKeyFrame KeyTime="0">
             <DiscreteObjectKeyFrame.Value>
              <System:Boolean>True</System:Boolean>
             </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
           </ObjectAnimationUsingKeyFrames>
          </Storyboard>
         </VisualState>
        </VisualStateGroup>
       </VisualStateManager.VisualStateGroups>

        <Grid>
         <Border x:Name="ReadOnlyVisualElement" Background="#5EC9C9C9" Opacity="0"/>
         <Border x:Name="MouseOverBorder" BorderBrush="Transparent" BorderThickness="1">
          <ScrollViewer x:Name="ContentElement" BorderThickness="0" IsTabStop="False" Padding="{TemplateBinding Padding}"/>
         </Border>
        </Grid>

       <Border x:Name="DisabledVisualElement" BorderBrush="#A5F7F7F7" BorderThickness="{TemplateBinding BorderThickness}" Background="#A5F7F7F7" IsHitTestVisible="False" Opacity="0"/>       
       <Border x:Name="ValidationErrorElement" BorderBrush="#FFDB000C" BorderThickness="1" CornerRadius="1" Visibility="Collapsed">
        <ToolTipService.ToolTip>
         <ToolTip x:Name="validationTooltip" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" >
          <ToolTip.Triggers>
           <EventTrigger RoutedEvent="Canvas.Loaded">
            <BeginStoryboard>
             <Storyboard>
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsHitTestVisible" Storyboard.TargetName="validationTooltip">
               <DiscreteObjectKeyFrame KeyTime="0">
                <DiscreteObjectKeyFrame.Value>
                 <System:Boolean>true</System:Boolean>
                </DiscreteObjectKeyFrame.Value>
               </DiscreteObjectKeyFrame>
              </ObjectAnimationUsingKeyFrames>
             </Storyboard>
            </BeginStoryboard>
           </EventTrigger>
          </ToolTip.Triggers>
         </ToolTip>
        </ToolTipService.ToolTip>
        <Grid Background="Transparent" HorizontalAlignment="Right" Height="12" Margin="1,-4,-4,0" VerticalAlignment="Top" Width="12">
         <Path Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z" Fill="#FFDC000C" Margin="1,3,0,0"/>
         <Path Data="M 0,0 L2,0 L 8,6 L8,8" Fill="#ffffff" Margin="1,3,0,0"/>
        </Grid>
       </Border>
      </Grid>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
  </Style>
那请放手 2024-09-09 19:17:13

将属性 Readonly 设置为 true

Set the property Readonly to true

¢蛋碎的人ぎ生 2024-09-09 19:17:13

您看到的是焦点元素。它是文本框默认模板的一部分。制作自己的模板是控制元素样式的最佳方法。只需删除主边框和焦点元素的边框,就可以了。然后,您也不再需要在 XAML 中内联设置边框粗细。

What you are seeing there is the Focus element. It's part of the default template for textboxes. Making your own template is the best way to get control of your element's style. Just remove the main border and the border of the focus element, and you should be good to go. Then you won't need to set the border thickness inline in your XAML anymore either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文