Here is what I am trying to do. I can't seem to be able to figure out a solution yet:
- I have 2 string data sources from a webservice and on the front end I want to combine 开发者_高级运维them into a single sentence separated by a comma.
- The first part I want in normal font, the second part I want in italics
- I also want the sentence to word wrap
- I am using MVVM so I want to some how bind these string data sources to a textblock some how...
The cases I want to be able to handle:
Normal:
ex. This is the sentence part 1, this is the sentence part 2No second part so no comma
ex. This is the sentence part 1 I still want this to be able to word wrapThe second part word wraps
ex. This is the sentence part 1, this is the sentence part 2 wrapped to second lineAlso
- Second Part only with word wrapping
- First part wordwraps followed by the second part
There seems to be no easy solution. The one that I can think is have a PropertyChangedEventHandler which notifies me when those strings were returned from the webserver, then format the string in codebehind...
In order to achieve the effect that you are trying to achieve continuous word wrapping with the italic formatting can only sensibly be achieved by using the <Run>
element within a TextBlock
. However, you cannot bind the contents of a Run
element, so you will need to create this in code.
Assuming that you get two separate responses from the web service (they don't come back from a single call), then you cannot rely on which one will come in first, so I would use event aggregation to notify the view from the view model when all the necessary data has been received. The PRISM library for WP7 includes an implementation that you can use to achieve the effect (note that PRISM for WP7 is much lighter than it's WPF or Silverlight counterparts).
In response to the event, your view can pull the properties from the view model, create the TextBlock
and it's component Run
elements, and then update accordingly. In fact, when you subscribe to an event using PRISM, you can specify that the handler for that event is run on the UI thread, which will no doubt help.
精彩评论