TextBlock Alignment inside a radiobutton Windows Phone - c#

I want to create a textbox inside a radiobutton in Windows Phone 7 in order to show multi-line text.
RadioButton rb = new RadioButton();
rb.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
rb.VerticalAlignment = System.Windows.VerticalAlignment.Center;
rb.Height = 104;
rb.Width = 396;
TextBlock txt = new TextBlock();
txt.TextWrapping = TextWrapping.Wrap;
txt.Height = 72;
txt.FontSize = 22;
txt.Width = 300;
txt.VerticalAlignment = System.Windows.VerticalAlignment.Center;
txt.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
txt.Text = "Some Text";
rb.Content = txt;
The Problem is that the Text in the Textblock can be either long or short. If it is long , it is fine. But if it is short, it aligns to the top border of the textblock, and it looks ugly. But I can't decrease the height of the textblock, because there will be not enough space in case of 2 or 3 lines of text.
How can I solve this problem?

You can set Height of the textbox to double.NaN in your code like txt.Height = double.NaN;.

you can set maxheight
make layout adapts the actual height of textblock by setting right alignment value

Related

Cannot set Text on button

I am trying to set Text on button.
Here is my code:
btn = new Button();
btn.Content = "A";
btn.Height = '*';
btn.Width = 200;
btn.Margin = new Thickness(10);
btn.Padding = new Thickness(100);
btn.VerticalAlignment = VerticalAlignment.Center;
btn.SetValue(Grid.ColumnProperty, column);
btn.SetValue(Grid.RowProperty, row);
Everything is working fine except I can't display text on button.
Please guide me in the right direction!
As your width is 200 and your padding (which shrinks the content's space from all directions) is 100, there is no more space for your content. Try using a smaller (or no) padding.

Why ClipToBounds = false not work?

I do not want cut text of textblock. For this reason, I set viewBox.ClipToBounds to false, But it doesn't work.
Please tell me why ClipToBounds=false not work in this code:
private void Btn1_Click(object sender, RoutedEventArgs e)
{
Button button = new Button(); button.Background = Brushes.Red;
button.Width = 70; button.Height = 20;
Canvas.SetLeft(button, 100); Canvas.SetTop(button, 120);
button.Padding = new Thickness(1);
StackPanel stackPanel = new StackPanel();
Viewbox viewBox = new Viewbox();
viewBox.ClipToBounds = false;
Canvas canvas = new Canvas();
canvas.Width = button.Width; canvas.Height = button.Height;
TextBlock textBlock = new TextBlock();
textBlock.Text = "this is a test";
textBlock.FontSize = 15;
textBlock.FontFamily = new FontFamily("Arial");
textBlock.TextWrapping = TextWrapping.NoWrap;
textBlock.Foreground = Brushes.Green;
textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
textBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
viewBox.Height = 20;
textBlock.IsHitTestVisible = false;
stackPanel.Children.Add(viewBox);
viewBox.Child = canvas;
canvas.Children.Add(textBlock);
button.Content = stackPanel;
Canvas MainCanvas = new Canvas();
MainCanvas.Children.Add(button);
this.Content = MainCanvas;
}
Screenhsot:
The screenshot below is what I want. :
ClipToBounds is false by default. However, clipping can still happen due to the way certain elements perform layout. Basically the way things work in WPF is that setting ClipToBounds = true will force things to clip. Leaving it set to false means that WPF determines how things should clip based on measure constraints and arrange rects.
If you look at the ArrangeCore and MeasureCore methods in FrameworkElement, you will see that there is quite a bit of logic determining whether something should clip. Of course, things that override FrameworkElement are free to render however they want, but generally they will obey the clipping rules established by the base class.
In the case of a TextBlock, it will definitely clip text that goes outside of its bounds if its size is constrained. You can see this by simply setting a Width on it, or placing it is a parent that has a Width set on it.
If you really need the text to render outside of the bounds of the control, you may have to consider something like writing a custom text rendering element.
Even then, it is still going to be clipped by its parent as soon as you place it in something else that clips. So, you could still end up stuck.
You could try placing the TextBlock on top of the button instead of inside of it, and setting its position to get it in the right place (maybe by binding it to something). This would work, but might get hard to manage if you need to do it too much.
Basically, you are trying to go against one of the hard-coded rules of WPF, so you are likely not going to find an easy way to do it. Perhaps you might want to reevaluate your design and determine if this behavior is really necessary for what you want to do, or if you can go about it in a different way.
Thanks to elgonzo and Xavier.
I realized that I should not put the canvas in the viewbox.
By 2 change my problem solved.
1 - Swap viewbox with canvas.
2 - Remove canvas.with = ...
This is correct code :
private void Btn1_Click(object sender, RoutedEventArgs e)
{
Button button = new Button(); button.Background = Brushes.Red;
button.Width = 70; button.Height = 20;
Canvas.SetLeft(button, 100); Canvas.SetTop(button, 120);
button.Padding = new Thickness(1);
StackPanel stackPanel = new StackPanel();
Viewbox viewBox = new Viewbox();
viewBox.ClipToBounds = false;
Canvas canvas = new Canvas();
// canvas.Width = button.Width; canvas.Height = button.Height;
TextBlock textBlock = new TextBlock();
textBlock.Text = "this is a test";
textBlock.FontSize = 15;
textBlock.FontFamily = new FontFamily("Arial");
textBlock.TextWrapping = TextWrapping.NoWrap;
textBlock.Foreground = Brushes.Green;
textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
textBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
viewBox.Height = 20;
textBlock.IsHitTestVisible = false;
stackPanel.Children.Add(canvas);
viewBox.Child = textBlock;
canvas.Children.Add(viewBox);
button.Content = stackPanel;
Canvas MainCanvas = new Canvas();
MainCanvas.Children.Add(button);
this.Content = MainCanvas;
}

Width of C# Label before it is shown?

I need to know which width the label will have with AutoSize = true; before the form is shown so I can position other controls relatively to the label. Actually I have no access to the form only to the parent Control.
(Working completely without Designer. That means just code.)
(Measure string in Graphics is unreliable so I can't use that.)
Label label = new Label();
label.AutoSize = true;
label.Location = new Point(x, y);
label.Text = "hello world";
myParent.Controls.Add(label);
// more control generation follows, THEN form is shown
Ok, you cannot get the label width before adding it into its parent control. just place the location after Controls.Add
Label label = new Label();
label.AutoSize = true;
label.Text = "hello world";
myParent.Controls.Add(label);
label.Left = cntControl1.Left - label.Width;
label.Top = cntControl1.Top;

WPF grid cells in C# no text

I need to show some data in a structured way with colored letters and rows with background colors.
I made a grid in a WPF Window. It shows the textboxes and some of the labels, but none of the text. Also the column header, last column, gridseperators, grid bot and left edges are invisible.
My grid is called propertiesView.
Code for adding header elements (labels)
private void AddHeaderElement(string text, int row, int col)
{
Label headerElement = new Label();
headerElement.Height = cellHeight;
headerElement.Width = cellWidth;
headerElement.DataContext = text;
headerElement.Background = headerBackground;
headerElement.BorderBrush = new SolidColorBrush(Color.FromRgb(120, 120, 120));
headerElement.BorderThickness = new Thickness(3);
propertiesView.Children.Add(headerElement);
Grid.SetRow(headerElement, row);
Grid.SetColumn(headerElement, col);
}
Code for adding cells
RichTextBox cell = new RichTextBox();
cell.Height = cellHeight;
cell.Width = cellWidth;
cell.ToolTip = toolTip;
cell.DataContext = text;
cell.Background = rowDifferent;
propertiesView.Children.Add(cell);
//box.SetValue(Grid.RowProperty, rowCount);
//box.SetValue(Grid.ColumnProperty, columnCount);
Grid.SetRow(cell, rowCount);
Grid.SetColumn(cell, columnCount);
Code for adding grid seperators
GridSplitter colSeperator = new GridSplitter();
colSeperator.Margin = new Thickness(-2.5, 0, 0, 0);
colSeperator.Width = 5;
colSeperator.ResizeDirection = GridResizeDirection.Columns;
colSeperator.ResizeBehavior = GridResizeBehavior.CurrentAndNext;
colSeperator.VerticalAlignment = VerticalAlignment.Stretch;
colSeperator.HorizontalAlignment = HorizontalAlignment.Left;
propertiesView.Children.Add(colSeperator);
Grid.SetColumn(colSeperator, 0);
Grid.SetRowSpan(colSeperator, totalRows + 1);
The tooltips do show the right text.
I tried using TextBox instead of RichTextBox and setting all this stuff in the class constructor instead of a seperate method.
Well it seems like you just never set the Content dependency property on your labels, or the Document of your RichTextBoxes.
For your labels, as you set the text parameter as the DataContext, you can just add something like
headerElement.SetBinding(Label.ContentProperty, new Binding());
Turns out I needed labels with textblocks, spans and runs.
Style can be added on the span (through properties like Foreground, TextDecoration or FontWeight). Add the text to the span inside a Run, then add all spans to a textblock, which is shown through a label.
Span span = new Span();
span.Foreground = Brushes.Black;
span.Inlines.Add(new Run("Text"));
textBlock.Inlines.Add(span);
Label cell = new Label();
cell.MinHeight = cellHeight;
cell.MaxWidth = cellWidth * 3;
cell.MinWidth = cellWidth;
cell.ToolTip = "toolTip";
cell.BorderThickness = new Thickness(2);
TextBlock cellText = new TextBlock();
cellText.HorizontalAlignment = HorizontalAlignment.Stretch;
cellText.TextWrapping = TextWrapping.WrapWithOverflow;
cell.Content = cellText;
The text works now, I should be able to get the gridseperators working.

How to center a Textbox inside a Border in C#

In my WP7 app, I am creating a Textbox inside a Border. How to align the Textbox exactly at the center of the Border?
Border rectangleborder = new Border();
rectangleborder.Background = new SolidColorBrush(Colors.Transparent);
rectangleborder.BorderBrush = new SolidColorBrush(Colors.Black);
rectangleborder.BorderThickness = new Thickness(2);
rectangleborder.Width = width;
rectangleborder.Height = width;
TextBox textbox = new TextBox();
textbox.Text = "1";
textbox.Background = new SolidColorBrush(Colors.Transparent);
textbox.Foreground = new SolidColorBrush(Colors.Yellow);
textbox.BorderBrush = new SolidColorBrush(Colors.Transparent);
this.canvas1.Children.Add(rectangleborder);
rectangleborder.SetValue(Canvas.LeftProperty, 30 + (j - 1) * width);
rectangleborder.SetValue(Canvas.TopProperty, 30 + (i - 1) * width);
rectangleborder.Child = textbox;
TextBox textbox = new TextBox();
textbox.HorizontalAlignment = HorizontalAlignment.Center;
textbox.VerticalAlignment = VerticalAlignment.Center;
You can also allign the text inside using:-
textBox.TextAlign = HorizontalAlignment.Center;
You need set the HorizontalAlignment to align horizontally and the VerticalAlignment to align vertically:
TextBox textbox = new TextBox();
textbox.HorizontalAlignment = HorizontalAlignment.Center;
textbox.VerticalAlignment = VerticalAlignment.Center;
And the result should look something like this:

Categories

Resources