How to Control Z Order of Grids - c#

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.

Related

UWP C# - Font auto size of TextBlock and RichTextBlock

How to force TexBlock and RichTextBlock to change FontSize so that the text fits in the window. When changing the text, if the text is too large, then it does not fit in the window. How to make it fit completely?
viewBox is not suitable because "TextWrapping=Wrap" does not work in it
Update:
I have a TextBlock for the entire width and height of the window. The user can change the text of any length. I would like to force TextBlock to change FontSize so that all the text fits in the window. viewBox is not suitable because "TextWrapping=Wrap" does not work in it.
I wrote a temporary code. But it's a terrible code. I would like to find a more correct code:
var textBlockHeight = Convert.ToInt32(page.ActualHeight - textBlock.Margin.Bottom);
var textBlockWidth = Convert.ToInt32(page.ActualWidth - (textBlock.Margin.Right * 2));
var canvas = new CanvasTextFormat
{
FontSize = 150,
WordWrapping = CanvasWordWrapping.Wrap,
};
var device = CanvasDevice.GetSharedDevice();
var layout = new CanvasTextLayout(device, text, canvas, textBlockWidth, 0);
var textHeight = (int)layout.DrawBounds.Height;
while (textBlockHeight < textHeight)
{
layout = new CanvasTextLayout(device, text, canvas, textBlockWidth, 0);
textHeight = (int)layout.DrawBounds.Height;
canvas.FontSize--;
if (canvas.FontSize <= 3)
{
break;
}
}
textBlock.FontSize = canvas.FontSize;
textBlock.Text = text;

How can I detect Borderside of MouseEnter event in C#/WPF for resizing Grid Elements

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.

WPF textblock resize dependant on max width and split

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.

I have made a border inside of a wrap panel programatically, but now I need to add 2 label into that border. How can I do that programatically as well

So currently I have this:
for (int i = 0; i < 100; i++)
{
WrapPanel.Children.Add(new Border { Background = (Brush)bc.ConvertFrom("#C7DFFC"), Margin = new System.Windows.Thickness(5, 5, 5, 5), Height = 50, Width = 50, Name = ("RAM" + i) });
}
This creates 100 borders inside my WrapPanel. Now I need to create 2 labels in each of those borders. How would I go about doing this?
Border border = new Border();
StackPanel panel = new StackPanel();
//add child controls like labels etc
panel.Children.Add(child1);
panel.Children.Add(child2);
//set the panel as child of border
border.Child = panel;
//nod add border to
WrapPanel.Children.Add(border);

Unable to retrieve width of dynamically loaded label with autosize as true

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.

Categories