wpf 如何将 TextBlock 设为可选,以便用户可以复制其文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12720096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make TextBlock as selectable so that user can copy its text
提问by D J
Possible Duplicate:
Any way to make a WPF textblock selectable?
可能的重复:有什么
方法可以使 WPF 文本块可选?
Can I make a textblock selectable in WPF application so that a user can copy it.
我可以在 WPF 应用程序中选择一个文本块,以便用户可以复制它。
Thanks in advance.
提前致谢。
DJ
DJ
回答by Chris W.
You could just make it into a TextBox that's Read Only which just looks like a TextBlock, kind of like;
你可以把它变成一个只读的 TextBox,它看起来像一个 TextBlock,有点像;
<Style x:Key="ReadOnlyTextBox" TargetType="TextBox">
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="Padding" Value="5"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="VerticalScrollBarVisibility" Value="Disabled"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid x:Name="RootElement">
<ScrollViewer x:Name="ContentElement"
Margin="{TemplateBinding Margin}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
IsTabStop="{TemplateBinding IsTabStop}"
Padding="{TemplateBinding Padding}"
HorizontalScrollBarVisibility="{TemplateBinding HorizontalScrollBarVisibility}"
VerticalScrollBarVisibility="{TemplateBinding VerticalScrollBarVisibility}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The ScrollViewer ContentElement would be in a TextBox by default, you could substitute for a ContentPresenter instead if you like also.
默认情况下,ScrollViewer ContentElement 将位于 TextBox 中,如果您愿意,也可以替换为 ContentPresenter。
Then put it into effect;
然后付诸实施;
<TextBox Text="Blah Blah Blah you can copy me!" Style="{StaticResource ReadOnlyTextBox}"/>
Hope this helps!
希望这可以帮助!
ADDENDUM:As @doodleus pointed out in the comments. Template binding the Content Property within the template may be necessary. As "ContentElement" is a named partof the Silverlight TextBox control. One of the little nuance differences to watch for in the different xaml Variants. I must not have paid attention to the Tags when I originally created the example. So kudos to him for correcting me.
附录:正如@doodleus 在评论中指出的那样。可能需要在模板中绑定内容属性的模板。因为“ContentElement”是Silverlight TextBox 控件的命名部分。在不同的 xaml 变体中需要注意的细微差别之一。当我最初创建示例时,我一定没有注意标签。所以感谢他纠正我。

