2011-03-10 57 views
3

我想在我的WPF應用程序內部使用一些措辭,但是我希望能夠使用某種程度的格式。FlowDocument來源於外部資源

我最初的想法是使用它代表一個的FlowDocument或段落的字符串資源,如:

<FlowDocument> 
    <Paragraph FontSize="16" Foreground="Blue">Some display text under content management</Paragraph> 
</FlowDocument> 

在UI我一直在嘗試這種使用的IValueConverter綁定:

<ContentControl Content="{Binding Path=CMSText,Source={StaticResource Resources},Converter={StaticResource flowDocConverter}"/> 

在轉換器:

StringReader sr = new StringReader(value.ToString()); 
XamlReader xamlReader = XamlReader.Create(sr); 
return (FlowDocument)xamlReader.Parse(); 

但它一直拋出一個例外返回st atement。

它甚至可以通過綁定來做到這一點?

我在XamlReader中出錯了嗎?

編輯

XamlParseException
'無法創建未知類型 '的FlowDocument'。'行號「1」和行位置「2」。

+0

你得到的例外文本是什麼? – David 2011-03-10 15:09:10

回答

1

我想說你根本無法將xamlReader.Parse()的結果轉換爲FlowDocument(我不知道爲什麼)。

你倒是應該嘗試這樣的事情作爲你的轉換器:

FlowDocument myFlowDoc = new FlowDocument(); 
myFlowDoc.Blocks.Add(new Paragraph(new Run(value))) 

return myFlowDoc; 

(我發現FlowDocument的管理缺乏簡單,而且往往是一個麻煩)

+0

我已更新異常詳細信息 – benPearce 2011-03-11 00:22:11

+0

好的問題,這證實了我所寫的內容,即:xamlParsing無法創建FlowDocument。問題是:爲什麼?我會看看,並嘗試重現這個想法。 – David 2011-03-11 07:36:50

+0

實際上,我看到的越多,我越理解你在做什麼:p ...你認爲CMSText是什麼?你明確地將它設置爲你之前提到的資源嗎?我不明白你的綁定和上面提到的flowDoc資源之間的聯繫。因爲問題不在於轉換器中的「價值」設置不正確(轉換器本身似乎沒有損壞,xamlParser也沒有,所以我認爲這是不好的值)。你可以通過轉換器檢查「value」變量包含的內容嗎? – David 2011-03-11 08:47:30

2

更改輸入字符串的FlowDocument標籤,添加NamePpace像這樣:

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:MARS"> 
    <Paragraph FontSize="16" Foreground="Blue">Some display text under content management</Paragraph> 
</FlowDocument>