-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?

11
  • 1
    Also, how about ToString() XD Commented May 28, 2011 at 21:50
  • so why doesn't SO let me post questions? why do i have to fillabuster in order to post it? Commented May 28, 2011 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") Commented May 28, 2011 at 22:44
  • then reread my original post and see what i was looking for. textblock.text isn't what i was looking for. Commented May 28, 2011 at 22:48
  • 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. Commented May 28, 2011 at 23:00

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

13 Comments

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: 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)
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.
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.
"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.
|
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.