I have a couple of CheckBoxes with a TextBlock as content.
Now I want to read out the TextBlock.Text from each Checkbox.
If I read out the content like checkBox.Content.ToString(); I only get System.Windows.Controls.TextBlock which kinda makes sense.
I also tried to create a new TextBlock and give it the content but it didn't work.
TextBlock _tempTBL = new TextBlock();
_tempTBL = checkBox.Content;
Any help is much appreciated.
var _tempTBL = (TextBlock) checkBox.Content; //Get handle to TextBlock
var text = _tempTBL.Text; //Read TextBlock's text
Edit:
On a side note, you can directly set desired text as CheckBox's content.
checkBox.Content = "Hello World";
And when you want to access the text, no type cast is needed
string text = checkBox.Content;
You have to cast the type to a TextBlock:
// no need to 'new' it up if you're assigning an existing instance...
TextBlock _tempTBL = (TextBlock) checkBox.Content;
Related
I am using this simple example from MSDN
to insert lines in a RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();
Run myRun = new Run("This is flow content and you can ");
Bold myBold = new Bold(new Run("edit me!"));
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun);
myParagraph.Inlines.Add(myBold);
myFlowDoc.Blocks.Add(myParagraph);
RichTextBox myRichTextBox = new RichTextBox();
myRichTextBox.Document = myFlowDoc;
I want to apply a chosed color to the lines of text, but how to do it?
The Paragraph or Run classes doesn't have any direct method to change the color.
EDIT
I don't want to use all the awkard SelectionStart, SelectionEnd stuff as posted on the linked post!.
My case is different and is much more simple: the solution posted from mm8 explains it and is very elegant.
One single line of code and that is!
Please see the answer!
The Paragraph or Run classes doesn't have any direct method to change the color.
The Run class inherits from TextElement and this class has a Foreground property that you can set to a Brush:
Run myRun = new Run("This is flow content and you can ") { Foreground = Brushes.Red };
Bold myBold = new Bold(new Run("edit me!") { Foreground = Brushes.Gray });
You can get/set text color via Foreground property of the rich text box. As bellow example, I changed the text color of rich text box to blue:
myRichTextBox.Foreground = Brushes.Blue;
Happy coding!
I'm using GemBox.Document to generate an output document from a template. I want to insert an image inside a TextBox which will have the same size as that TextBox.
How can I do that?
DocumentModel document = DocumentModel.Load("mytemplate.dotx");
TextBox textBox = (TextBox)document.GetChildElements(true, ElementType.TextBox).First();
Picture picture = new Picture(document, "myimage.png");
textBox.Blocks.Add(new Paragraph(document, picture));
Try the following:
DocumentModel document = DocumentModel.Load("mytemplate.dotx");
TextBox textBox = (TextBox)document.GetChildElements(true, ElementType.TextBox).First();
// If needed you can adjust the TextBox element's inner margin to your requirement.
textBox.TextBoxFormat.InternalMargin = new Padding(0);
// If needed you can remove any existing content from TextBox element.
textBox.Blocks.Clear();
// Get TextBox element's size.
var textBoxSize = textBox.Layout.Size;
// Create and add Picture element.
textBox.Blocks.Add(
new Paragraph(document,
new Picture(document, "myimage.png", textBoxSize.Width, textBoxSize.Height)));
I hope this helps.
I found out I can do
((GridView)myListView.View).Columns[i].Width = x;
to change the width. But how to Change the Header text? I can't find anything like
((GridView)myListView.View).Columns[i].Content = "Hello World";
You just need to change the header property;
((GridView)myListView.View).Columns[i].Header = "Hello World";
I created some labels in a for loop on the behind code.
At the beginning it looks like that:
private void SlotLabelCreation(string name)
{
Label label = new Label();
label.Name = name;
label.HorizontalAlignment = HorizontalAlignment.Left;
label.VerticalAlignment = VerticalAlignment.Top;
label.Content = "[Free Slot]";
label.Foreground = Brushes.Gray;
label.BorderBrush = Brushes.Black;
label.BorderThickness = new Thickness(1);
label.Visibility = Visibility.Hidden;
MainGrid.Children.Add(label);
//the margin has been inserted later in other code.
}
everything was fine but when I inserted to the label other content which not contains the same amount of letters it looks like that:
sorry about the links.. it's because I can't upload images
http://s27.postimg.org/6halp6p37/pic2.png
I wanted to make all slots the same size so I added a MinWidth property to the labels.
The new result was:
http://s27.postimg.org/6z5r51eo3/pic3.png
now it looks better but I am wondering how could I center the content which inside the label.
Unfortunately I didn't find any solution to solve this problem.
p.s, HorizontalAlignment and VerticalAlignment don't solve the problem.
Thanks a lot, Alon P.
You should use HorizontalContentAlignment and VerticalContentAlignment properties
I am adding tabs to my tab control through code:
TabItem tab = new TabItem();
var stack = new StackPanel() { Orientation = Orientation.Horizontal };
stack.Children.Add(new TextBlock() { Text = header });
stack.Children.Add(new TextBlock() { Name = "extra" });
tab.Header = stack;
tabControl.Items.Add(tab);
As you can see, it creates the header of the tabItem with a stack panel. It adds two text blocks; one of which is empty, but I've assigned the name "extra". What I would like to do is, later in the code, edit the textBlock named "extra" and add some new text to it.
How would I find and edit this element? I have tried the following code, but its producing an error saying the element can not be found:
object test = Application.Current.FindResource("extra");
FindName is what you are looking for but your TextBlock is not in the correct WPF Namescope.
MSDN states:
If you add an object to an object tree at a point in time after the XAML that produced that tree was parsed, a Name or x:Name value on the new object does not automatically update the information in a XAML namescope. To add a name for an object into a WPF XAML namescope after XAML is loaded, must call the appropriate implementation of RegisterName on the object that defines the XAML namescope.
For example:
var textBlock = new TextBlock() { Name = "extra" };
stack.Children.Add(textBlock );
RegisterName(textBlock);
...
TextBlock textBlock = FindName("extra") as TextBlock;
Finally, Application.Current.FindResource("extra") is returning null because the element does not exist when project resources are created. More on FindResource.
Just use FrameworkElement.FindName method:
var control = tab.FindName("extra");
if(control is TextBlock){
// your logic here
}
You don't need Application.Current.Resource dictionary here because it's different collection. If you want to use it then you should put user controls within Resource dictionary beforehand.
Because you are trying to find resource with the key "extra". It's wrong.
Try this:
TabItem tab = new TabItem();
var stack = new StackPanel() { Orientation = Orientation.Horizontal };
var textBlock = new TextBlock() { Name = "extra" }
stack.Children.Add(new TextBlock() { Text = header });
stack.Children.Add(textBlock);
tab.Header = stack;
tabControl.Items.Add(tab);
Now you can reach it with textBlock instance.
Here's some VB WPF code for what you need
Dim test As TextBlock
test = DirectCast(FindName("extra"), TextBlock)
I have no idea if it will work like that in C# WPF although if that doesn't work try looking up CType