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

时间:2010-05-23 14:18:06

标签: c# silverlight textbox textblock cut-and-paste

我想在 Silverlight 应用程序中显示文本,以便用户可以在其他地方复制并粘贴(就像用于 HTML < / strong>网站)。

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

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

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

效果很好:

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

但是,当用户悬停在文本框上选择文字时,会出现另一个边框

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

我发现purported solutions用于删除此边框,但令人难以置信地需要页面的XAML。

我正在寻找像这样的简单解决方案

HoverBorderThickness="0"

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

6 个答案:

答案 0 :(得分:5)

不幸的是,你曾经遇到过想要做一些复杂似乎相对简单的事情的事情,但是你必须跳过一些箍来做这件事。如前面的答案所述,需要重新设置/重新模板文本框。

至于悬停状态 - 这是你应该掌握的相对较大的Silverlight点 - 它的想法是每个控件都有单独的状态

Expression Blend是处理这些状态的绝佳方式 - 您可以轻松查看内置XAML模板和样式的内容,并且可以轻松地即时编辑它们。有时可能会很棘手,而且肯定会让你的XAML变得有点笨重,但这就是你需要它来控制做你想做的事情所花费的成本。

利用国家的力量 - 特别是在Blend中 - 我保证你会意识到你的生活会变得多么容易。

答案 1 :(得分:2)

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

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);
    }
}

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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

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

答案 4 :(得分:1)

将属性Readonly设置为true

答案 5 :(得分:0)

你看到的是Focus元素。它是文本框默认模板的一部分。制作自己的模板是控制元素风格的最佳方式。只需删除主要边框和焦点元素的边框,你就应该好好去。然后,您不再需要在XAML中设置内联边框粗细。