我试图在运行时使用runobject创建一个TextBlock。 我需要文本块来显示以下文字:
请 点击此处 转到下一页。
点击这里应该是一个“链接”,用户可以点击它,他将移动到下一个tabitem ..
我有:var tbGoToNextTab = new TextBlock
{
Content = "Please Click Here to go to next Page.",
Foreground = new SolidColorBrush(Colors.Black)
};
tbGoToNextTab .Click +=new RoutedEventHandler(tbGoToNextTab_Click);
我如何点击这里Clickable,有下划线和蓝色文字颜色并点击它时执行一个动作?
提前致谢
我只想要2个单词“Click Here”可点击...其余的应该显示为普通文本.. 我想我应该用Inlines做这个...有什么建议吗?
答案 0 :(得分:2)
这样的东西? (另)
var hyperlink = new HyperLink(new Run("Click Here"));
hyperlink.Click += new RoutedEventHandler(tbGoToNextTab_Click);
var span = new Span();
span.Inlines.Add(new Run("Please "));
span.Inlines.Add(hyperlink);
span.Inlines.Add(new Run(" to go to next Page"));
var tbGoToNextTab = new TextBlock
{
Content = span,
Foreground = new SolidColorBrush(Colors.Black)
};
答案 1 :(得分:1)
好的找到答案......
因为我认为我必须使用Inline属性..
我在这篇文章中找到了答案:
Add hyperlink to textblock wpf From Stanislav Kniazev
谢谢大家......
答案 2 :(得分:0)
有类似的东西.. Textblock作为按钮中的内容..因此可点击。
Canvas pnel = new Canvas();
var btntab = new Button();
var tbGoToNextTab1 = new TextBlock();
var tbGoToNextTab2 = new TextBlock();
var tbGoToNextTab3 = new TextBlock();
tbGoToNextTab1.Text = "Please ";
tbGoToNextTab2.Text = "Click Here";
tbGoToNextTab3.Text = " to go to next Page.";
tbGoToNextTab1.Margin = new Thickness(0, 0, 0, 0);
btntab.Margin = new Thickness(40, 0, 0, 0);
tbGoToNextTab3.Margin = new Thickness(95, 0, 0, 0);
tbGoToNextTab1.Foreground = new SolidColorBrush(Colors.Black);
tbGoToNextTab2.Foreground = new SolidColorBrush(Colors.Black);
tbGoToNextTab3.Foreground = new SolidColorBrush(Colors.Black);
btntab.Click += new RoutedEventHandler(btntab_Click);
btntab.Content = tbGoToNextTab2;
pnel.Children.Add(tbGoToNextTab1);
pnel.Children.Add(btntab);
pnel.Children.Add(tbGoToNextTab3);
Text112.Children.Add(pnel);
答案 3 :(得分:0)
要管理左键单击操作,请在PreviewMouseLeftButtonDown
tbGoToNextTab.PreviewMouseLeftButtonDown += OntbGoToNextTabPreviewMouseLeftButtonDown;
void OntbGoToNextTabPreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
// write code here...
}
要管理下划线和颜色更改,请在xaml中写一个Style
,在此样式中添加一个Trigger
,它会更改TextBlock
属性更改中的IsMouseOver
。< / p>
<Window.Resources>
<Style x:Key="HyperLinkStyle" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="TextDecorations" Value="Underline" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
tbGoToNextTab.Style = FindResource("HyperLinkStyle") as Style;
您必须专门为可点击文本和静态文本的其他TextBlock声明一个TextBlock。
答案 4 :(得分:0)
如果您处理Click
的{{1}}事件,它将处理控件边界内的任意位置。
如果您只需要部分文字可点击,则应在TextBlock
的正文中放置Hyperlink
。顺便说一下,突出显示可点击区域还有一个额外的好处,这样您就不会与那些无法点击的“点击此处”的文字混淆。
以下是相关的WPF代码段:
TextBlock