Hello I have a textblock within a grid row. The text outside the width of the row is not shown. How can i split the text and display it underneath?
//Actual -->
//Date: 06/06/2018 realy long text is displayed, b
//Wanted -->
//Date: 06/06/2018 realy long text is displayed,
//but not showing everything due to width
//isue
Grid DynamicGrid = new Grid();
DynamicGrid.Width = 450;
DynamicGrid.Height = 1000;
DynamicGrid.HorizontalAlignment = HorizontalAlignment.Left;
DynamicGrid.VerticalAlignment = VerticalAlignment.Top;
DynamicGrid.ShowGridLines = false;
RowDefinition gridRow;
gridRow = new RowDefinition();
gridRow.Height = new GridLength(50);
DynamicGrid.RowDefinitions.Add(gridRow);
TextBlock txtBlock1 = new TextBlock();
txtBlock1.Text = "06/06/2018 realy long text is displayed, but not showing everything due to width isue";
txtBlock1.FontSize = 20;
txtBlock1.VerticalAlignment = VerticalAlignment.Top;
Grid.SetRow(txtBlock1, 0);
Grid.SetColumn(txtBlock1, 0);
DynamicGrid.Children.Add(txtBlock1);
Setting the TextWrapping property of the TextBlock to wrap will cause the textblock to show full text.
Related
Is it possible to set for each side of a border its own EventHandler for mouse-enter or mouse-leave event. For example for the Left-Border of a Grid and Top-Border of a Grid?
What I am actually trying to do is allow the user to resize Grid-Elements inside a Canvas that contain a TextBlock with the mouse.
I am inserting my Grid/Border into the Canvas with the following code:
Border border = new Border();
border.BorderThickness = new Thickness(2);
border.BorderBrush = Brushes.Black;
TextBlock tb = new TextBlock();
tb.HorizontalAlignment = HorizontalAlignment.Stretch;
tb.TextWrapping = TextWrapping.Wrap;
tb.Padding = new Thickness(5, 5, 5, 5);
tb.Text = fd.LabelText;
Grid grid = new Grid();
grid.Background = labelBackgroundBrush;
grid.Background.Opacity = myOpactiy;
border.DataContext = fd;
grid.Children.Add(tb);
border.Child = grid;
I found a good example at csharphelper.com . Although my implementation is still buggy this was a good inspiration for me. Maybe it can help others who want to do the same.
I am trying to edit this code (scroll at the end of the post), I want that number has a normal format and it has alignment, margin, etc... Here what I changed:
GetVisualChildCollection<DataGridRow>(dataGrid).
ForEach(d => {
Style style = new Style(typeof(DataGridRowHeader));
style.Setters.Add(new Setter(DataGridRowHeader.HorizontalContentAlignmentProperty, HorizontalAlignment.Right));
TextBlock textBlock = new TextBlock();
textBlock.Margin = new Thickness(5, 0, 5, 0);
textBlock.HorizontalAlignment = HorizontalAlignment.Right;
textBlock.Text = (d.GetIndex() + 1).ToString();
d.Header = textBlock;
d.HeaderStyle = style;
});
The margin is present. But I have a problem with alignment.
I want to align text at textbox by the right of the row header.
I tried to set the alignment of textbox and row header style but nothing works out.
That is how I want.
So I did.
I am trying to create resizable textboxes (so when the window is resized the textbox adjusts itself) programatically but the text inside the textbox is always very small in comparison to the textbox:
TextBox textBox = new TextBox();
textBox.Name = name;
textBox.Text = text;
textBox.SetValue(Grid.ColumnProperty, column);
textBox.SetValue(Grid.RowProperty, row);
textBox.SetValue(Grid.ColumnSpanProperty, columnspan);
textBox.SetValue(Grid.RowSpanProperty, rowspan);
So i added a binding to keep the text at the same size as the textbox:
Binding b = new Binding();
b.RelativeSource = new RelativeSource(RelativeSourceMode.Self);
b.Path = new PropertyPath(TextBox.ActualHeightProperty);
textBox.SetBinding(TextBox.FontSizeProperty, b);
but when i do this the text becomes way too big for the textbox.
What am i doing wrong/missing here?
You are binding the fontsize which is pixels to actual height which is in device independent units so its apples to oranges.
Is there any reason to not use a viewbox?
Something like (I'm doing this freehand so beware..no ide may have typos)
TextBox textBox = new TextBox();
Viewbox vb = new Viewbox();
vb.Child = textbox;
vb.Stretch = Uniform;
textBox.Name = name;
textBox.Text = text;
vb.SetValue(Grid.ColumnProperty, column);
vb.SetValue(Grid.RowProperty, row);
vb.SetValue(Grid.ColumnSpanProperty, columnspan);
vb.SetValue(Grid.RowSpanProperty, rowspan);
I a bit stuck here. I am trying to dynamically add panel with two labels in it.
The first label must be auto sized and the second label should be positioned to the left of the first one which has a fixed max width and its also auto sized. My code is a follows
pnlSearchResults.SuspendLayout();
Panel pnlRow = new Panel
{
AutoSize = true,
BorderStyle = BorderStyle.FixedSingle,
Padding = new Padding(0),
Margin = new Padding(0),
Top = 0,
Left = 0,
Width = pnlSearchResults.Width - 30,
MaximumSize = new Size(pnlSearchResults.Width - 30, 0)
};
Label lblKey = new Label
{
Name = string.Format("lblKey{0}", 1),
Text = "My label goes here",
AutoSize = true,
TextAlign = ContentAlignment.MiddleLeft,
BorderStyle = BorderStyle.FixedSingle,
Padding = new Padding(0),
Margin = new Padding(0),
Top = 0,
Left = 0,
};
pnlRow.Controls.Add(lblKey);
Label lblValue = new Label
{
Name = string.Format("lblValue{0}", 1),
Text = "And my long text goes here... and it goes on and on and on and on and on and on and on and on and on and on and ...",
AutoSize = true,
Height = 27,
TextAlign = ContentAlignment.MiddleLeft,
BorderStyle = BorderStyle.FixedSingle,
Padding = new Padding(0),
Margin = new Padding(0),
Top = 0,
MaximumSize = new Size(pnlRow.Width - lblKey.Width - 10, 0),
Left = lblKey.PreferredWidth
};
pnlRow.Controls.Add(lblValue);
pnlSearchResults.Controls.Add(pnlRow);
pnlSearchResults.ResumeLayout();
pnlSearchResults.PerformLayout();
I read that calling suspend, resume or perform methods increases performance. And this is what im getting.
As you can see the second label is not getting properly aligned with the first label. Its working fine if i set autosize = false for first label, but I want my first label to be auto sized.
I am not sure what I am missing here, please guide me in the proper way.
This occurs because the label has not yet been drawn and therefore adjusted itself to the correct size. If you want this to work you cannot suspend and resume layouts, you cannot place the controls before the form is shown and you will probably need to call a Refresh in between placing the labels to allow the 1st one to autosize correctly.
I have the following code:
TextBlock tmp = new TextBlock
{
Text = displayText,
Foreground = new SolidColorBrush(Colors.Red),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
FontSize = 30
};
Grid grd = new Grid();
grd.Children.Add(tmp);
// grd.Background = new SolidColorBrush(Colors.LightGray);
Viewbox vb = new Viewbox();
vb.Child = grd;
vb.Width = width;
vb.Height = height;
DrawingCvs.Children.Add(vb);
Canvas.SetLeft(vb, xpos);
Canvas.SetTop(vb, ypos);
Canvas.SetZIndex(grd, 1000);
// we need a second grid for cosmetic reasons. Due to
// how the viewbox works, when we resize we lose the full
// width of the grid which has a textblock in it
Grid cosmeticGrd = new Grid
{
Width = width,
Height = height,
Background = new SolidColorBrush(Colors.LightGray)
};
DrawingCvs.Children.Add(cosmeticGrd);
Canvas.SetLeft(cosmeticGrd, xpos);
Canvas.SetTop(cosmeticGrd, ypos);
Canvas.SetZIndex(grd, 0);
What I want is for the Grid added first in the code to be above the grid added second. What is wrong with my Z-Index property setting here? Or is it something else?
InB4 Change the order you add them in the code--yes I realize this, but as a point of understanding I want to understand the ZIndex property.
Your first SetZindex call:
Canvas.SetZIndex(grd, 1000);
incorrectly applies the setting to the Grid instead of the Viewbox. It should be:
Canvas.SetZIndex(vb, 1000);
because the Viewbox is the child of the Canvas.
Likewise, your second SetZIndex call:
Canvas.SetZIndex(grd, 0);
is applied to the wrong Grid. It should be:
Canvas.SetZIndex(cosmeticGrd, 0);
In summary, the Canvas.ZIndex attached property affects the ordering of Canvas children.