-4

How do you convert a TextBlock to a string in c# from wpf? i have a listbox that contains a textblock in order to change the foreground color. but when i get listbox.selecteditem, it obviously returns a textblock, but i need to know what the textblock is, converted into a string. how do i do that?

darthwillard
  • 809
  • 2
  • 14
  • 28
  • 1
    Also, how about `ToString()` XD – H.B. May 28 '11 at 21:50
  • so why doesn't SO let me post questions? why do i have to fillabuster in order to post it? – darthwillard May 28 '11 at 22:42
  • Because SO *totally knew* that your question was mundane and beyond trivial. (To be fair, in this context *"knew"* means *"heuristically determined with an error margin of x"*) – H.B. May 28 '11 at 22:44
  • then reread my original post and see what i was looking for. textblock.text isn't what i was looking for. – darthwillard May 28 '11 at 22:48
  • Your question is not any bit clearer, define *"know what the textblock is"* first. – H.B. May 28 '11 at 22:50
  • this is what i'm trying to do http://stackoverflow.com/questions/6161744/how-is-the-foreground-color-changed-on-just-one-line-of-wpf-listbox – darthwillard May 28 '11 at 22:51
  • textblock: TextBlock tb = new TextBlock(); tb.Text = trimmedString; tb.Foreground = new SolidColorBrush(Colors.Red); lbxFileList.Items.Add(tb); – darthwillard May 28 '11 at 22:52
  • when i select the listbox item it RETURNS A TEXTBLOCK because that's what is added to the listbox. – darthwillard May 28 '11 at 22:53
  • Well, then **don't do that**! See additional edits in my answer. – H.B. May 28 '11 at 22:57
  • i'm looking to change the color of a single item in a listbox. just one. not the whole box, not the selected item, not the background of the selected item. if the item added is a jpeg, i want it to be green. if the item is an mpg, i want it to be red. the only way i achieved this is by using a textblock. but when the user clicks on an item in the listbox, i need to know what they clicked on, but translated into a string. – darthwillard May 28 '11 at 22:58
  • 1
    I am not going to repeat myself. Also SO's wisdom was confirmed, your question was bad since you absolutely failed to express your problem appropriately. – H.B. May 28 '11 at 23:00

2 Answers2

8

The following region exists for historical reasons.


TextBlock.Text?

Edit: To quote the documentation i linked to:

TextBlock textBlock = new TextBlock();
textBlock.Text = "The text contents of this TextBlock.";

If you still cannot think of any way to get the text out of a TextBlock now then... i don't even know what then.

Edit:

...know what the textblock is...

This is about as vague as you can get.


Begin Answer (Since this was not apparent)

Based on some of your comments you apparently try to recostruct information based on the TextBlock you get from a ListBox. TextBlocks do (and should) not contain object state information, if you have more information than just text you should create a new class with the respective properties, bind to a collection of such objects, and datatemplate the collection appropriately.

That way the SelectedItem will be an object of the class which contains the information you need, and if you edit that information the UI will reflect those changes. TextBlocks are not homeopathic devices.

End Answer

From what answer you accepted on this question it looks to me like you just don't want to bother learning about data binding and all the things that make WPF such a great platform. Way to cling to error-prone imperative UI creation.


Take 9001:

 string text = ((TextBlock)listbox.SelectedItem).Text;

This?

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • i'm not using a textbox. +1 for clever link to a wikipedia page to convey an idea that has nothing to do with a logical answer. i wish i could give you two upp'd reps. – darthwillard May 28 '11 at 23:11
  • @darthwillard: Way to jump on a typographical error, please consider the possibility that you are simply approaching this the wrong way and that i am actually trying to help here. (Also i cannot see any upvote) – H.B. May 28 '11 at 23:13
  • if you wanted to help, you'd read what i wrote in the comments about what i'm trying to accomplish. i'm trying to add items to a listbox and change the foreground color of different ones. i don't need people to point out the obvious that the original question has nothing to do with it. i tried a different approach to the problem and tried using a textblock, but it wasn't returing a string for me. i tried `string x = lbxSomeListbox.SelectedItem.ToString()` but it didn't work because the selected item was a textblock and not a string. – darthwillard May 28 '11 at 23:26
  • Also: *"if you wanted to help, you'd..."*, just no. If i **had not** wanted to help i wouldn't have answered in the first place. – H.B. May 28 '11 at 23:34
  • i apologize if i went with the answer that helped me the most to that other question i asked. i'm sorry if the answer you gave me was over my head. you see, as a beginner, on more than one occasion, if you ask for clarification on an answer someone will tell you "to look it up yourself", "if you can't figure out the answer, you should start with something more basic", etc. i simply checked the answer that i understood, and the one that helped me the most in my code. i can see that my not choosing your answer was a bone of contention between us though. – darthwillard May 29 '11 at 00:07
  • 1
    *"i can see that my not choosing your answer was a bone of contention between us though."* actually that is not the case, as i only noticed that after quite some time and it did not have any effect on my already slightly unnerved attitue, i just don't like questions that are almost impossible to answer because they are vague or fail to even express the problem at hand properly. As you might be able to see this absolutely nothing to do with TextBlocks, nor their conversion to strings, it's just about basic C# knowledge related to casting/boxing/unboxing objects. – H.B. May 29 '11 at 00:16
  • Choosing the answer which helped you the most is perfectly fine, but you might also want to consider the long time effects, my answer has the highest upvote count for a reason, it's a statement by the community that it is the best answer. So while you may not be able to grasp it right away it might be very well worth your time to read through the articles i linked to to understand the concepts behind it which are very helpful, not only in this particular problem but in any WPF application you may work on. – H.B. May 29 '11 at 00:20
  • I don't think posting your email address here is a good idea, unless you trust the gmail spam filter enough. Anyway, the code required to remove the buttons again that can be seen in the accepted answer is something you should avoid, it's not exactly elegant if you literally have to find it again just to get rid of it. Further the code is imperative, this normally has two effects: The code requires more steps and it is a linear thread which makes it harder to read and spot errors. – H.B. May 29 '11 at 01:15
  • Further data binding is often really helpful since it takes care of the synchronization between model and view, if you approach this imperatively you have to check all kinds of conditions in which you need to update the view manually. Also if you for example need to change the functionality of the button and you use event handlers you may quickly end up with a memory leak, if the whole logic is encapsulated and you simply change the command in the model that cannot happen. – H.B. May 29 '11 at 01:16
  • By getting rid of it i meant if you want to remove a certain button from the panel again. Sorry, i do not have the time to explain stuff any more extensive than this, if you don't know what imperative programming is, [have a look at wikipedia](http://en.wikipedia.org/wiki/Imperative_programming). – H.B. May 29 '11 at 01:33
  • @darthwillard: Just wondering, is my answer still lacking anything as you said earlier that i already supplied the answer you were looking for but you did not accept it? – H.B. May 31 '11 at 00:57
  • Thank you for `(listbox.SelectedItem as TextBlock).Text`. I was having a fit trying to figure out why I couldn't do `variable.Text` in my `foreach`. This helped me, tremendously. – doubleJ Jan 25 '14 at 21:07
  • @doubleJ: Note that you should not use [`as`](http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx) in this way. Only use it if you check for `null` afterwards, otherwise use a normal cast, e.g. `var tb = (TextBlock)listbox.SelectedItem; var text = tb.Text;`. I only used `as` here because you can get away with less parentheses if you one-line it, the normal cast one-liner would be `var text = ((TextBlock)listbox.SelectedItem).Text;`. – H.B. Jan 26 '14 at 04:20
2

You can't convert a TextBlock to a string. If you mean the content, look H.B.'s answer.

ibram
  • 4,414
  • 2
  • 22
  • 34