Is it possible to have a readonly WPF textbox display the first 3 characters one color and the remaining 7 characters a different color. Best example I can give with markdown is 201103/478.
The reason the client has asked for this is because stock numbers have the following format YYYYMM/999. 90+% of the time the client refers to the stock number in YMM/999 format. The re开发者_如何学编程quest is the YYY characters are displayed in light grey and the remaining YMM/999 characters are displayed standard black.
Currently a textbox control is used as the stock number must be able to be copied to the clipboard and allow sorting via a context menu. If another control is more suitable to fulfill the coloring, copy & sort requirements then that solution is acceptable.
@Vlad lead me in the right direction to come up with the following answer.
<TextBlock HorizontalAlignment="Right">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Sample"/>
</ContextMenu>
</TextBlock.ContextMenu>
<Span Foreground="Gray"><Run Text="{controls:Binding Path=StockNumberPrefix}"/></Span><Run Text="{controls:Binding Path=ShortStockNumber}" Margin="0"/>
</TextBlock>
Are you sure you need exactly a TextBox
? If you could use TextBlock
, you would be able to put the first 3 characters into a different Span
(which can have its own Foreground
brush). This way you won't have the possibility to set the text through bindings, though.
(You can restyle TextBlock
to look like a TextBox
, if this matters.)
精彩评论