Add multiple lines to TextBlock, WPF app - wpf

I'm making a WPF app and I want to add strings in ingredientsList to a TextBlock. But from my code below, It seems like the Textblock regconizes only the last string in the list. How can a display all strings on my list to the text box? or any suggestion for using other control instead of a TextBlock?
TextBlock txbDisplayIngredients = new TextBlock ();
List<string> ingredientsList = new List<string>();
for (int t = 0; t < ingredientsList.Count(); t++)
{
txbDisplayIngredients.Text = ingredientsList[t] + "\n";
}

It's because you're basically changing it in each iteration, and the only one you see is the last one ...
assuming you have 3 string one,two,three, you're running and telling it: "Oy, make it one", then "make it two", then "make it three", so it'll do it, and you'll end up with the text box saying: "three"
You want to either have all your strings as one multi line string, or use an observable collection of strings that you'll bind that textblock to.
You could also append the lines to the Inlines property of the TextBlock: more on this msdn page

I just found out, it works if i use this:
txbDisplayIngredients.Inlines.Add(" + " + ingredientsList[t] + "\n");

Related

WPF - How to populate TreeView with directory path strings

I can't find a solution to add TreeViewItems to a WPF TreeView hierarchically. Each string is as follows:
\\localhost\C$\Application\<ServiceN>\folder\file.doc
If there are multiple strings with the same <ServiceN>, I would like them to be added under the same ServiceN TreeViewItem. I would like to display the items like this:
localhost
ServiceN
C$\Application\ServiceN\folder
file.doc
file2.doc
ServiceN2
C$\Application\ServiceN2\folder
fileN.doc
So far, I have tried by using a List with a ItemsSource like this:
BindingList<string> treeItems = new BindingList<string>();
listView.ItemsSource = treeItems;
elsewhere in the code:
foreach(string f in paths) {
treeItems.Add(f);
}
However, that will display the items as simple string items without any depth/level. I also used multiple nested for loops, but that does not seem as the most efficient way. How can I parse each path string and add it into the correct TreeView position?

WPF UserControl does not update contents

I create an Window and stylezed it with MahApps, i also created an UserControl. In my UserControl i create a method that populate some data in the UserControl elements.
In my window, i created a button that do the following:
EdicaoFisioterapeuta ed = new EdicaoFisioterapeuta();
ed.LoadContents("My Text");
when i debug the code, i see the elements of user control being populated, here is the code:
public void LoadContents(string text)
{
textBox1.Text = text;
lbl.Text = text;
}
After all the job, the textBox1 and the lbl does not get the content "My Text".
I create another Project and repeated the process, everything is working fine in the new project.
In my "old" project i removed MahApps, but ir doesnt get effect.
I know, it is so simple, but i cant find an solution for this trouble.
Oh man, i was instatiating the wrong class, it seems i need to sleep a little more.

How can I preserve tabs in a Silverlight RichTextBox?

I've overridden the key-handling events and am inserting tabs when a user presses the tab key for a RichTextBox.
When I save the XAML from that RichTextBox and reload it, all of the tabs are now spaces.
Does anyone know what I can do to get the RichTextBox to display tabs?
I got around this issue by putting a placeholder in temporarily.
private const string TAB = " ";
private const string TAB_PLACEHOLDER = "===TAB===";
I used the placeholder to temporarily replace all of the tab characters and then once they were in the RichTextBox I replaced all of the placeholders with tabs.
textBox1.Text = richTextBox1.Xaml;
string xaml = richTextBox1.Xaml;
xaml = xaml.Replace(TAB, TAB_PLACEHOLDER);
richTextBox2.Xaml = xaml;
foreach (Block block in richTextBox2.Blocks)
{
foreach (Inline inline in ((Paragraph)block).Inlines)
{
((Run) inline).Text = ((Run) inline).Text.Replace(TAB_PLACEHOLDER, TAB);
}
}

WPF flowdocument element names are reset?

I have a flowdocument with a named Span" test1" which I want to replace the contents of programmatically
TextRange tr = new TextRange(this.test1.ContentStart,this.test1.ContentEnd);
Run run = this.test1.Inlines.First() as Run;
run.Text = "replaced text";
This works, however the problem is when doing the update the name of the span is removed (looking at the xaml source). Is this by design? Is there any way to retain the Id's?
Hi, this is the method I use for debugging whats actually in the richtextbox (setting xaml source to textbox)
using (MemoryStream ms = new MemoryStream())
{
tr = new TextRange(this.richTextBox1.Document.ContentStart, this.richTextBox1.Document.ContentEnd);
tr.Save(ms, DataFormats.Xaml);
ms.Position = 0;
using (StreamReader sr = new StreamReader(ms))
{
this.textBox1.Text = sr.ReadToEnd();
}
}
This is the test contents of the richtextbox:
<FlowDocument>
<Paragraph>
This is my first <Span Name="test1">a huge amount of space</Span> between it and the second paragraph?
</Paragraph>
</FlowDocument>
Unfortunately, this is just the way FlowDocuments work (same with tags). The only (very hacky) solution I have found for this is to hide a name in the FontFamily property. This works because this property is a comma delimited list of strings, with the first string that matches an existing font family being used, and the rest being ignored. So if you do something like this:
document.FontFamily = "RealFont1, RealFont2, name=test1";
The whole string will be preserved. You may then access the "name" by searching the FontFamily for "name=".
Again, quite hacky, but after months of trying it was the only solution I could find.

Set the text and value of a ComboBoxItem

I'm trying to populate a ComboBox programatically. I am creating ComboBoxItems and would like to set their text (the text that is visible for the end-user) and their value (the object that I will handle in the background after the user has selected it.
However the ComboBoxItem seems to only have one member for these two requirements: the Content variable. At the same time this would not fit my needs as I want to distinguish the text and value properties and want to do this without data binding. Is there some viable solution to achieve this?
My current code looks as follows:
ComboBox comboBox;
ComboBoxItem item = new ComboBoxItem();
item.Content = "First Item";
item.Value = 1; // Does not work, no such member as Value!
comboBox.Items.Add(item);
Guess you can use the Tag property.

Resources