2016-09-17 18 views
0

親愛的stackoverflow社區!文本框:混合顏色和數據綁定

我試圖找到解決我的問題,但我找不到任何。

問題:

我有一個綁定到一個串一個多行TextBox。每當我給字符串添加一些東西時,我想改變新部分的顏色,只有

到底應該應該是這樣的:

Textbox:

(你的想法)

這是到目前爲止我的文本框:

<TextBox x:Name="ChatTextBox" Text="{Binding MyStringProperty , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap" 
      AcceptsReturn="True"     
      IsReadOnly="True"></TextBox> 
      </ScrollViewer> 

當搜索一個解決方案,我看到一些使用Inline的例子,但對我來說,Inline看起來不能動態。

<TextBlock TextWrapping="Wrap"> 
    <TextBlock.Inlines> 
     <Run Foreground="Blue" Text="The first line in the Textbox is blue"/> 
     <Run Foreground="Green" Text="then I add this part and it is green"/> 
     <Run Foreground="Orange" Text="and this part is orange" /> 
    </TextBlock.Inlines> 
</TextBlock> 

我不一定要綁定到一個字符串,如果需要任何其他對象(或目錄),這將不會是一個問題。

任何幫助,高度讚賞!先謝謝你。

回答

0

你可以使用一個RichTextBox控制和使用段落和運行的,請在此:

<RichTextBox x:Name="richTextBox" Margin="10,128.08,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"> 
     <FlowDocument> 
      <Paragraph> 
       <Run Text="The first line in the Textbox is blue" Foreground="Blue"/> 
       <Run Text="{Binding Text, ElementName=textBox}" Foreground="Brown"/> 
      </Paragraph> 
      <Paragraph> 
       <Run Text="then I add this part and it is green" Foreground="Green"/> 
      </Paragraph> 
      <Paragraph> 
       <Run Text="and this part is orange" Foreground="Orange"/> 
      </Paragraph> 
     </FlowDocument> 
    </RichTextBox> 
+0

謝謝您的回答。但據我所見,在這種情況下代碼仍然與3行的長度綁定。 如果我的文字長到10行怎麼辦? – Al0x