2

我想在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 文本框上的悬停边框的简单方法是什么?

4

6 回答 6

5

不幸的是,您遇到过想要做一些综合起来看起来相对简单的事情,但是您必须跳过几个圈才能做到。如前面的答案中所述,您需要按原样重新设置/重新设置文本框的样式。

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

Expression Blend 是处理这些状态的绝佳方式 - 您可以轻松查看内置 XAML 模板和样式是什么,并以最小的努力即时编辑它们。有时可能会很棘手,而且它肯定会使您的 XAML 有点笨重,但这就是您让控件在您想要的时候完全按照您想要的方式执行的成本。

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

于 2010-05-24T19:20:17.317 回答
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);
    }
}
于 2011-12-12T22:13:23.990 回答
1

将属性设置Readonlytrue

于 2011-10-18T20:25:58.237 回答
1

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

于 2010-05-23T21:23:55.153 回答
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>
于 2010-05-24T06:45:55.203 回答
0

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

于 2010-05-23T21:30:47.950 回答