2017-04-03 40 views

回答

2

我和你有同样的问题。 我希望我的标签可以选择。

我没有找到一个合适的方法来做到这一点,而是使用了一个自定义样式的TextBox。

<Style x:Key="TextBoxAsLabel" TargetType="{x:Type TextBox}"> 
    <Setter Property="BorderBrush" Value="Transparent"/> 
    <Setter Property="BorderThickness" Value="0"/> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="Padding" Value="0"/> 
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/> 
    <Setter Property="HorizontalContentAlignment" Value="Left"/> 
    <Setter Property="VerticalAlignment" Value="Center"/> 
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
    <Setter Property="AllowDrop" Value="False"/> 
    <Setter Property="FontFamily" Value="Arial"/> 
    <Setter Property="HorizontalAlignment" Value="Stretch" /> 
    <Setter Property="VerticalAlignment" Value="Center" /> 
    <Setter Property="TextWrapping" Value="Wrap"/> 
    <Setter Property="FontSize" Value="11"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TextBox}"> 
       <themes:ClassicBorderDecorator x:Name="Bd" BorderThickness="0" BorderStyle="Sunken" Background="{TemplateBinding Background}"> 
        <ScrollViewer x:Name="PART_ContentHost" BorderBrush="Transparent" BorderThickness="0"/> 
       </themes:ClassicBorderDecorator> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsReadOnly" Value="true"> 
         <Setter Property="Background" TargetName="Bd" Value="Transparent"/> 
         <Setter Property="Foreground" Value="Black"/> 
         <Setter Property="BorderThickness" Value="0,0,0,0"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

你还需要添加到您的命名空间:

xmlns:themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Classic" 

用法为:<TextBox Text="{Binding ValueToBind}" IsReadOnly="True" Style="{DynamicResource TextBoxAsLabel}" />

注:根据需要改变你的风格绑定类型。

希望这会帮助你:)

1

你不应该重写整个模板。试试这个:

<TextBox Text="Copy this..."> 
    <TextBox.Style> 
     <Style TargetType="TextBox"> 
      <Setter Property="IsReadOnly" Value="True" /> 
      <Setter Property="BorderThickness" Value="0" /> 
      <Setter Property="TextWrapping" Value="Wrap" /> 
     </Style> 
    </TextBox.Style> 
</TextBox> 

上面的样式应该给你一个选择TextBox,看起来像一个TextBlockLabel