嵌套的TextBlocks和超链接,你如何复制这一XAML在C#中? [英] Nested TextBlocks and Hyperlinks, How do you replicate this XAML in C#?

查看:168
本文介绍了嵌套的TextBlocks和超链接,你如何复制这一XAML在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的XAML:

 < TextBlock的TextWrapping =自动换行前景=绿色
文本=这是一些绿色的文字前面。>
< TextBlock的前景=蓝>
这是一些蓝色文本。
< / TextBlock的>
这是继蓝文青的一些文字。
<&超链接GT;
< TextBlock的文本=最后,这是一个超链接。 TextWrapping =自动换行/>
< /超链接>
< / TextBlock的>

和我想知道如何在程序上复制它在C#。

$ B



<$ p:
$ b

我知道如何创建的TextBlock S IN C#的东西,如$ p> 的TextBlock TB =新的TextBlock();
tb.Text =一些文字

和我可以把多个的TextBlock 取值一起在C#中的面板。但我不看你怎么去把的TextBlock s转换其他的TextBlock s和的TextBlock s转换超链接 s转换的TextBlock 秒。



都是些容器对象和额外的的TextBlock 对象被创建自动,不知何故?抑或是的TextBlock 有一些方法/属性,允许它包含其他项目



其他相关问题?结果
1.什么是添加的东西就像一个点击()事件的超链接的最佳方式?结果
2.有一种方式来获得蓝色文字更清洁包?在上面的XAML,只要最右边的字就需要包装,蓝色文本的整个块被包裹吧。



感谢您的任何照明您可以提供。


解决方案

您可以修改内联的通过的的TextBlock的内联财产。以上XAML样品看起来有点像这样:

 的TextBlock TB =新的TextBlock 
{
文本=这是一些绿色文本前面。,
=前景Brushes.Green
};

InlineCollection tbInlines = tb.Inlines;

tbInlines.Add(新运行
{
文本=这是一些蓝色文本,
TextWrapping = TextWrapping.Wrap,
=前景Brushes.Blue
});

tbInlines.Add(新运行
{
文本=这是继蓝文青的一些文字。
});

运行hyperlinkRun =新的运行(最后,这是一个超链接。);

tbInlines.Add(新超链接(hyperlinkRun));



至于你的相关问题:



1A)虽然可以挂钩的事件处理程序使用的类RequestNavigate事件的每一个超链接的实例,这可能是昂贵的安装和使用更多的内存比是必要的。相反,我建议利用路由事件,只是挂钩的容器,其中所有的超链接将是RequestNavigate事件。你可以这样做像这样:

  myContainer.AddHandler(
Hyperlink.RequestNavigateEvent,
新RequestNavigateEventHandler(
(发件人,参数)=>
{
/ *发件人是在这里单击超链接的实例* /
}));



2A)在您的XAML例如它的治疗在内的TextBlock作为一个需要一起包装元素。如果您在使用我的基于运行方法包装应包含文本块继承。


I have this XAML:

<TextBlock TextWrapping="Wrap" Foreground="Green"
           Text="This is some Green text up front. ">
    <TextBlock Foreground="Blue">
        This is some Blue text.
    </TextBlock>
    This is some Green text following the Blue text. 
    <Hyperlink>
        <TextBlock Text="And finally, this is a Hyperlink." TextWrapping="Wrap"/>
    </Hyperlink>
</TextBlock>  

And I'd like to know how to replicate it procedurally in C#.

I know how to create TextBlocks in C# with something like:

TextBlock tb = new TextBlock();
tb.Text="Some text"

And I can put multiple TextBlocks together in a panel within C#. But I don't see how you go about putting TextBlocks into other TextBlocks, and TextBlocks into Hyperlinks into TextBlocks.

Are some container objects and extra TextBlock objects being created automatically, somehow? Or does the TextBlock have some methods/properties that allow it to contain the other items?

Other related questions:
1. What's the best way to add something like a Click() event to the Hyperlink?
2. Is there a way to get the blue text to wrap more cleanly? In the above XAML, as soon as the rightmost word would need to wrap, the entire block of blue text is wrapped instead.

Thanks for any illumination you can provide.

解决方案

You can modify the collection of Inlines exposed via the TextBlock's Inlines property. The above XAML sample would look a little something like this:

TextBlock tb = new TextBlock
               {
                  Text = "This is some Green text up front.",
                  Foreground = Brushes.Green
               };

InlineCollection tbInlines = tb.Inlines;

tbInlines.Add(new Run
              {
                 Text = "This is some Blue text.",
                 TextWrapping = TextWrapping.Wrap,
                 Foreground = Brushes.Blue
              });

tbInlines.Add(new Run
              {
                 Text = "This is some Green text following the Blue text."
              });

Run hyperlinkRun = new Run("And finally, this is a Hyperlink.");

tbInlines.Add(new Hyperlink(hyperlinkRun));

As for your related questions:

1A) While it's possible to hook an event handler to every single Hyperlink instance using the RequestNavigate event on the class, that can be costly to setup and use more memory than is necessary. Instead I recommend leveraging routed events and simply hooking the RequestNavigate event at the container where all of your hyperlinks will be. You can do this like so:

myContainer.AddHandler(
                     Hyperlink.RequestNavigateEvent, 
                     new RequestNavigateEventHandler(
                                                      (sender, args) =>
                                                      {
                                                        /* sender is the instance of the Hyperlink that was clicked here */
                                                      }));

2A) In your XAML example it's treating the inner TextBlock as an element that needs to be wrapped all together. If you're using my Run based approach the wrapping should be inherited from the containing text block.

这篇关于嵌套的TextBlocks和超链接,你如何复制这一XAML在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆