简体   繁体   English

绑定RichTextBox而不输入很多无用的代码或将HyperLinks添加到字符串?

[英]Bind RichTextBox without typing a lot of useless code or add HyperLinks to strings?

I had a class with <toolkit:ExpanderView> -elements. 我有一个<toolkit:ExpanderView> -elements类。 Those are bound to a ViewModel and need to stay bound, because I'm already using a lot of checks like "Which VM-element is expanded and which not", etc. 那些绑定到ViewModel并需要保持绑定,因为我已经在使用很多检查,例如“哪些VM元素已扩展而哪些未扩展”等。

The text inside of that expanded part was read from localized Strings (AppResources.resx) and added inside of the VM. 该扩展部分内的文本是从本地化字符串(AppResources.resx)中读取的,并添加到了VM内。 But now I have to add hyperlinks to that text. 但是现在我必须为该文本添加超链接。

My idea was to throw away the localized strings for a while and create RichTextBoxes inside of the ViewModel. 我的想法是扔掉本地化字符串一段时间,然后在ViewModel内创建RichTextBoxes。 Instead of binding strings to the expanded part I would bind the RichTextBoxes. 与其将字符串绑定到扩展部分,不如绑定RichTextBoxes。

But all methods I find suck. 但是我发现所有方法都很糟糕。 eg: 例如:

StackPanel tempGrid = new StackPanel();
StackPanel tempGrid = new StackPanel();
RichTextBox tempRtb = new RichTextBox();
Paragraph tempPara = new Paragraph();

Run tempRun = new Run();
Hyperlink tempHLink = new Hyperlink();
tempHLink.Click += Hyperlink_Click;

tempRun.Text = "line 1\nline 2\nline 3";
tempPara.Inlines.Add(tempRun);
tempRun.Text = "\nTel.:       ";
tempPara.Inlines.Add(tempRun);
tempHLink.Inlines.Add("+01800 000 000 000");
tempPara.Inlines.Add(tempHLink);
tempRun.Text = "\nE-Mail: ";
tempPara.Inlines.Add(tempRun);
tempHLink.Inlines.Add("e-mail@email.com");
tempPara.Inlines.Add(tempHLink);

But it fails already when I reuse tempRun . 但是当我重用tempRun时,它已经失败了。 Should I add thousands of new variables? 我应该添加数千个新变量吗? ôo Also the in the spec ( http://msdn.microsoft.com/en-us/library/system.windows.documents.run.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1 ) it says that something like 也是规格中的( http://msdn.microsoft.com/zh-cn/library/system.windows.documents.run.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1 )它说像

Run run = new Run("string");

should work. 应该管用。 But it doesn't with WP8 and turns to 但它不适用于WP8,并转向

Run run = new Run();
run.text = "string";

So everything becomes WAY longer... :( 所以一切都变得更长了... :(

Edit: 编辑:

Yes, forgot to ask the actual question: Is there a faster way to fill a richtextbox or paragraph with lots of Run or Hyperlink -elements? 是的,忘了问一个实际的问题:是否有一种更快的方法来用很多RunHyperlink元素填充Richtextbox或段落? And how do I bind it in my xaml? 以及如何将其绑定到我的xaml中? I need something like: 我需要类似的东西:

<DataTemplate>
    <RichTextBox Content="{Binding MyModelViewParagraph}"/>
</DataTemplate>

to bind my paragraph from the ViewModel to the xaml. 将我的段落从ViewModel绑定到xaml。

Okay, googling further lead me to this: 好的,谷歌搜索进一步将我引到了这一点:

ViewModel: ViewModel:

StackPanel tempStack = new StackPanel();
RichTextBox tempRtb = new RichTextBox();
Paragraph tempPara = new Paragraph();

//tempHLink.Click += Hyperlink_Click;

tempPara.Inlines.Add(new Run { Text = "text 1\ntext 2\n text 3" });
tempPara.Inlines.Add(new Run { Text = "\nTel.:       " });
//tempInline.Add(new Hyperlink { new InlineCollection {  =  } }) // "+000 (0) 00 00 00-0"
tempPara.Inlines.Add(new Run { Text = "\nE-Mail: " });
//tempInline.Add(new Hyperlink { new InlineCollection {  =  } }) // e-mail@email.com

tempRtb.Blocks.Add(tempPara);
tempStack.Children.Add(tempRtb);

Still need to find out how exactly I give Text and Click-Event to every HyperLink but that'll be managable I guess.. 仍然需要找出我如何为每个HyperLink精确地提供Text和Click-Event,但是我想这将是可管理的。

xaml: xaml:

<DataTemplate>
    <Grid>
        <ContentPresenter Content="{Binding Content}"/>
    </Grid>
</DataTemplate>

That binds my Stackpanel so I can add as many elements as I need. 这绑定了我的Stackpanel,因此我可以根据需要添加任意数量的元素。

Sorry for answering it by myself. 对不起,我自己回答。 Sometimes you seem to be stuck and it's better to ask then lose 2-3 more days on something that might be simple.. And sometimes asking the question leads you to a solution. 有时您似乎被困住了,最好先问一下,然后再损失2-3天,以解决可能很简单的事情。有时问问题会导致您找到解决方案。 In my case it was just mroe stupid googling and reading the MS-specification xD 就我而言,这只是愚蠢的谷歌搜索和读取MS规范xD

Edit: If someone ants to know the whole solutionf ro the Hyperlinks as well - I found out :) 编辑:如果有人想要知道整个解决方案以及超链接-我发现了:)

tempPara.Inlines.Add(CreateHyperlinkWithContent("+00 (00) 00 00 00-0"));

With the method: 用的方法:

private Hyperlink CreateHyperlinkWithContent(string content)
{
    Hyperlink tempHyperLink = new Hyperlink();
    tempHyperLink.Inlines.Add(content);
    tempHyperLink.Click += Hyperlink_Click;
    tempHyperLink.Foreground = new SolidColorBrush(Color.FromArgb(0xff, 0x33, 0x66, 0x99));
    tempHyperLink.MouseOverForeground = new SolidColorBrush(Color.FromArgb(0xff, 0x33, 0x66, 0x99));
    return tempHyperLink;
}

This is the shortest way I could find to add Text and Links in a single Line everytime ;) 这是我每次都能在单行中添加文本和链接的最短方法;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM