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);
}
}
Related
In WPF, I use tabcontrol have tabitems to create new Dynamic content. I have button Reset to clear all textboxes in All TabItems. My code is working with tab focus. But, When I click the other tab and click again old tab, The content show again. Please help me so that I can clear all textboxes in all tabItems
My Code here:
private void ClearControls(DependencyObject root)
{
System.Windows.Controls.TabControl controls = new System.Windows.Controls.TabControl();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
{
var control = VisualTreeHelper.GetChild(root, i);
if (control is System.Windows.Controls.TextBox)
{
(control as System.Windows.Controls.TextBox).Text = String.Empty;
}
else if (VisualTreeHelper.GetChildrenCount(control) > 0)
{
ClearControls(control);
}
}
}
private void BtnResetTurnbyTurn_Click(object sender, RoutedEventArgs e)
{
ClearControls(this);
}
Don't waste your time with tree visualizer
Design your ui in a separately usercontrol and give a name then from
Then head to your main window and give the area that is supposed to display the ui a name or just declare a grid and give it a name
Now head to your tabs and double click the one that is suppoused to show the ui you want and from the code just write the following
Just to clarify the names
I have my separately designd ui in a file called pages and it's name is myui and my usercontrol tag is name child
My viewing area is filled with a grid named
Mainview
I have double clicked the required tab and it drove me to the cs file inside the click event
My code is
` pages.myui mu =new pages.myui();
Mainview.Children.Clear();
Mainview.Cildren.Add(mu.child);`
It might be the most basic way to accomplish the mission but believe me it is a life saver
Comment for any question
I hope i have helped you
Yusuf naeem
I'm having some problems displaying the contents of a flowdocument in a flowdocumentscrollviewer. I create a generic list that holds a class which contains an int, string and a flowdocument.
In a WPF listbox, I am trying to display the flowdocument in the scrollviewer alongside a button. I use the following function called from the WPF window constructor to populate the listbox
private void populateListBox()
{
foreach(Element el in _notesList)
{
StackPanel sp = new StackPanel();
sp.Orientation = Orientation.Horizontal;
Button b = new Button();
b.Content = el._theID;
sp.Children.Add(b);
FlowDocumentScrollViewer fdsv = new FlowDocumentScrollViewer();
fdsv.MinWidth = 400;
fdsv.Document = el._theDoc;
sp.Children.Add(fdsv);
ListBoxItem lbi = new ListBoxItem();
lbi.Content = sp;
noteList.Items.Add(lbi);
}
}
But the code does not work. There are no errors but the scrollviewers are just blank in the listbox. I also tried storing the classes in an ObservableList and binding to the Document property but that didn't work either.
Any ideas what is happening?
Nevermind. I figured it out.
Further down in the program execution I was copying the flowdocument blocks to a merged document in a foreach statement. This doesn't work even if you use Blocks.ToList(). I eventually found a way to copy the document contents to another document here.
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");
Hey all i have created a few text boxes and also buttons to go along with them at run-time.
The code for the button is:
Dim updateButton As New Button
updateButton.Name = "button_" & ticketTheRowNum & "_" & ticketRowNum
updateButton.Content = "UPDATE!"
updateButton.Height = 26
Canvas.SetTop(updateButton, 24 * ticketTheRowNum)
Canvas.SetLeft(updateButton, 330 + lblNotes.Width)
updateButton.Width = lblNotes.Width / 2 - 10
updateButton.Background = New SolidColorBrush(Colors.Green)
Grid.SetRow(updateButton, 0)
Grid.SetColumn(updateButton, 0)
Grid.SetZIndex(updateButton, 2500)
cavTicket.Children.Add(updateButton)
And the code for the text box is this:
Dim txtBlock As New TextBox
txtBlock.Name = "txt_" & ticketTheRowNum & "_" & ticketRowNum
txtBlock.Text = theHeader
txtBlock.TextWrapping = TextWrapping.Wrap
txtBlock.Width = lblNotes.Width - 5
txtBlock.BorderThickness = New Thickness(0)
txtBlock.TextAlignment = TextAlignment.Left
txtBlock.VerticalScrollBarVisibility = ScrollBarVisibility.Auto
tmpExpander.Content = txtBlock
Now the button and text box displays just fine on the WPF but I am unsure of how to hook the button up to have it know whats in the text box and save its content. Currently since I am creating these at run-time I don't have access like I would if its was already on the form before it ran (I would simply just call the buttons click event and then within that I would call the text box content and save it).
If both of your code blocks come from the same class then just add a private property txtBlock (by the way why block and not Box?). So when you will create your TextBox control it will then be visible to all the functions in your class including the button event handler.
But as NETscape said. WPF is all about XAML, binding, and WVVM. So if you want to avoid headaches use them ;)
I saw how to set a WPF rich text box in RichTextBox Class.
Yet I like to save its text to the database like I used to, in Windows Forms.
string myData = richTextBox.Text;
dbSave(myData);
How can I do it?
At the bottom of the MSDN RichTextBox reference there's a link to How to Extract the Text Content from a RichTextBox
It's going to look like this:
public string RichTextBoxExample()
{
RichTextBox myRichTextBox = new RichTextBox();
// Create a FlowDocument to contain content for the RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();
// Add initial content to the RichTextBox.
myRichTextBox.Document = myFlowDoc;
// Let's pretend the RichTextBox gets content magically ...
TextRange textRange = new TextRange(
// TextPointer to the start of content in the RichTextBox.
myRichTextBox.Document.ContentStart,
// TextPointer to the end of content in the RichTextBox.
myRichTextBox.Document.ContentEnd
);
// The Text property on a TextRange object returns a string
// representing the plain text content of the TextRange.
return textRange.Text;
}