Silverlight 2 TextBlock ignores MaxWidth - silverlight

I am using Silverlight 2 to dynamically add a TextBlock to a Canvas. I set the MaxWidth of the TextBlock but it ignores this value and displays a string longer than the MaxWidth value.
TextBlock label=new TextBlock();
label.SetValue(Canvas.LeftProperty,Convert.ToDouble(x+3));
label.SetValue(Canvas.TopProperty, Convert.ToDouble(y + 1));
label.Width = DisplayWidth - 6;
label.Height = DisplayHeight - 2;
label.TextWrapping = TextWrapping.NoWrap;
label.MaxWidth = DisplayWidth-6;
label.MinWidth = DisplayWidth-6;
label.Text = this.Title;
label.Margin = new Thickness(3.0);
baseCanvas.Children.Add(label);
What do I have to do to get the TextBlock to restrict its width to a specific value? Ideally I'll add conditional ellipses too (i.e. ...).

It would seem that MaxWidth on a TextBlock is ineffectual when the TextBlock is a direct child of a Canvas. I can't quite fathom why that would be so. However the solution would be to place the TextBlock in a Border:-
TextBlock label=new TextBlock();
label.SetValue(Canvas.LeftProperty,Convert.ToDouble(x+3));
label.SetValue(Canvas.TopProperty, Convert.ToDouble(y + 1));
label.Width = DisplayWidth - 6;
label.Height = DisplayHeight - 2;
label.TextWrapping = TextWrapping.NoWrap;
label.MaxWidth = DisplayWidth-6;
label.MinWidth = DisplayWidth-6;
label.Text = this.Title;
label.Margin = new Thickness(3.0);
Border border = new Border();
border.Child = label;
baseCanvas.Children.Add(border);
The Border will honor the MaxWidth of the TextBlock but since it is given no thickness the border itself is invisible.

Related

Animate the width of a GridViewColumn

I would like to animate the width of a GridViewColumn. I can animate the GridViewColumnHeader and/or the contents of the column but neither have the desired effect. Since the GridViewColumn is not an UIElement I'm not sure how to do this. I can do something like:
var doubleAnimation = new DoubleAnimation();
doubleAnimation.From = 200;
doubleAnimation.To = 300;
doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1));
column.BeginAnimation(WidthProperty, doubleAnimation);
column.Column.Width = 300;
But this simply snaps the width out to 300 and the Header slowly grows out to 300.

about RichTextBox in silverlight4 or wpf

I have dynamically added some RichTextbox in my Silverlight app.
so, I want to get the actualHeight of these different RichTextBox for set my layout
code like this:
RichTextBox content = new RichTextBox();
content.TextWrapping = TextWrapping.Wrap;
content.Width = 800;
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(p.Content);
content.IsReadOnly = true;
content.Blocks.Add(paragraph);
content.FontSize = 14;
content.BorderThickness = new Thickness(0);
content.Name = "content" + i;
height.Add(content.ActualHeight);
But I find that the content.ActualHeight is always 0.0
So, I want to know how to get the actualheight of richtextbox.

Silverlight, calculate shape Height & Width using persentage

I have a silverlight application that works but I now need to resize some items using a percentage instead of using fixed Height and Width.
My code snippit for this is:
private void drawCell(int row, int col, string label, Color fill)
{
Rectangle rect = new Rectangle();
rect.Height = cellSize;
rect.Width = cellSize;
rect.StrokeThickness = 2;
rect.Stroke = new SolidColorBrush(Colors.DarkGray);
rect.Fill = new SolidColorBrush(fill);
Grid.Children.Add(rect);
Canvas.SetLeft(rect, col * cellSize);
Canvas.SetTop(rect, row * cellSize);
Rectangle innertec = new Rectangle();
innertec.Height = cellSize - 30;
innertec.Width = cellSize - 10;
innertec.StrokeThickness = 1;
innertec.Stroke = new SolidColorBrush(Colors.Black);
innertec.Fill = new SolidColorBrush(fill);
innertec.Margin = new Thickness(5);
Grid.Children.Add(innertec);
Canvas.SetLeft(innertec, col * cellSize);
Canvas.SetTop(innertec, row * cellSize);
Border productLabelBorder = new Border();
TextBlock productLabel = new TextBlock();
productLabel.Height = cellSize - 60;
productLabel.Width = cellSize - 10;
productLabel.Margin = new Thickness(5, innertec.Height + 5, 0, 5);
productLabel.TextAlignment = TextAlignment.Center;
productLabel.TextWrapping = TextWrapping.NoWrap;
productLabel.TextTrimming = TextTrimming.WordEllipsis;
productLabel.Text = label;
ToolTipService.SetToolTip(productLabel, label);
productLabelBorder.Child = productLabel;
Grid.Children.Add(productLabelBorder);
Canvas.SetLeft(productLabelBorder, col * cellSize);
Canvas.SetTop(productLabelBorder, row * cellSize);
}
What I want the code to do is take the innertec and productLabel, and calculate the Height and Width by first looking at the cellSize (which is a variable set elsewhere) and then create these objects with a percentage of cellSize.
The reason for this is that cellSize changed size depending on a slider in the UI. The "cells" resize after calculating the area of the Canvas.
Is it possible to calculate these values as a percentage?
Thanks in advance for your help.
It looks like you are not using your Grid correctly! I can see you are adding items to the Grid, then trying to position them via the Canvas attached properties. You are mixing two different panel types!
With the Canvas, children are positions by their Top and Left coordinates
With the Grid children are located within cells as indicated by the Grid.Row and Grid.Column attached property.
You can use 'star' widths and heights to define the proportional width and height of rows / columns in order have grid cells which are some percentage of the overall grid. For example in markup, if you want to add a grid cell which is 50% of the overall grid size you can do the following:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions
<Button Grid.Column="1" Content="foo"/>
</Grid>
With the above, the Button will have a width which is 50% of the overall Grid width.

How can I display multiple colored underlined Text in a Textbox

this code underlines all Text in the Textbox, can I underline only specific Text?
Brush brush = Brushes.Blue;
Pen pen = new Pen(brush,2);
TextBox tb1 = new TextBox();
tb1.AcceptsReturn = true;
tb1.Text = "This is a very long Text not?";
TextDecoration textDec = new TextDecoration(TextDecorationLocation.Underline,pen,1,TextDecorationUnit.Pixel,TextDecorationUnit.FontRecommended);
tb1.TextDecorations.Add(textDec);
tb1.Width = 400;
tb1.Height = 30;
this.AddChild(tb1);
The TextBox doesn't provide the ability to alter characteristics for individual characters. It's an all or none control.
The RichTextBox is the control you need.

WPF - Text box that grows vertically to accommodate all text

The problem: I am not getting a textbox setting that will have a horizontally wordwrap and vertically auto grow functionality. I wish to do that by writing a code. I have written following code that creates a text box at mouse dblclick with wordwrap:
TextBox text2 = new TextBox();
text2.Width = 500;
text2.Visibility = Visibility.Visible;
text2.Focus();
text2.Height = 30;
text2.HorizontalAlignment = HorizontalAlignment.Left;
text2.VerticalAlignment = VerticalAlignment.Top;
Point p = e.GetPosition(LayoutRoot);
text2.Margin = new Thickness(p.X, p.Y, 0, 0);
LayoutRoot.Children.Add(text2);
But, textbox does not grow vertically.
Can somebody suggest me a code in C# to do exactly what I desire?
try using this
Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid.RowDefinitions.Add(new RowDefinition());
TextBox textBox = new TextBox() { Width = 100, TextWrapping = TextWrapping.Wrap };
textBox.SetValue(Grid.RowProperty, 0);
grid.Children.Add(textBox);
window.Content = grid;
where window is the Name assigned to Window(root).
One way to accomplish the growth you're looking for is to use a string measuring mechanism which you would run any time the text in your text box changes. Simply measure and resize your text box accordingly with any change to the contents.
Have you tried this?
text2.Height = double.NaN; // or don't set the property, but some custom styles might give a default value ..
text2.TextWrapping = TextWrapping.Wrap;
text2.MinHeight = 30; // or not if you want the styles default
instead of
text2.Height = 30;
not setting it or using double.NaN is the same as using 'Auto' in xaml.

Resources