c# Expander content is not on the left - c#

I have the problem that the content of the expander does not start at the left, how can I solve this?
As you can see on the picture, the contents of my expander are not shown to the far left. I have tried many settings, unfortunately I do not come to the solution. The expander itself also seems to have an edge opposite the stackpanel, which I also want to avoid.
Above the expander you can see a grid, which contains a text block with content. This grid has the right mass within the higher-level stack panel.
Expander exp = new Expander
{
Header = "TestExpander",
Width = spStackPanel.Width,
ExpandDirection = ExpandDirection.Down,
IsExpanded = true,
BorderBrush = Brushes.Yellow,
BorderThickness = new Thickness(1),
HorizontalAlignment = HorizontalAlignment.Left,
HorizontalContentAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
VerticalContentAlignment = VerticalAlignment.Top,
};
TextBox tx = new TextBox
{
Text = "testText"
};
exp.Content = tx;
spStackPanel.Children.Add(exp);

I can't comment so apologies for the somewhat vague answer, but it might help to share the styling and XAML used to create your screenshot. By default that isn't how an Expander looks, so my guess is that something in your styling or one of the higher level controls (e.g. StackPanel or Grid) is affecting the layout of the Expander.
Example being possibly a keyless style for TextBox that applies padding and/or horizontal alignment, which would then affect the content you're adding by creating a new TextBox in your snippet to give it that odd alignment.

I have written a test here, directly in a .cs file WITHOUT XML:
buttonGrid = new Grid();
buttonGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });
buttonGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });
Expander exp = new Expander()
{
Header = "Test Expander",
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(0),
IsExpanded = true
};
TextBlock label = new TextBlock()
{
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(0),
Text = "Test Text"
};
exp.Content = label;
Grid.SetRow(exp, 0);
buttonGrid.Children.Add(exp);
TextBlock text = new TextBlock()
{
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(0),
Text = "Test Text",
Foreground = Brushes.White
};
Grid.SetRow(text, 1);
buttonGrid.Children.Add(text);
I know that an expander doesn't usually look like this, so I ask my question ;-)
maybe someone has me a tip to look for.

Related

Label not visible above ToolStrip

At runtime I add (and remove) several controls, as needed, to a main window which in Designer contains only a ToolStrip with some function buttons. In some cases I want to add an info label next to the toolStrip, but I cannot make it visible, ie. it is hidden below. The code for the label is straightforward
infoLabel = new Label();
infoLabel.AutoSize = true;
infoLabel.Location = new System.Drawing.Point(200, 10);
infoLabel.Size = new System.Drawing.Size(35, 13);
infoLabel.BackColor = System.Drawing.SystemColors.Control;
infoLabel.Font = new System.Drawing.Font("Arial", 13);
infoLabel.ForeColor = System.Drawing.Color.Black;
infoLabel.TabIndex = 1;
infoLabel.Text = "this is info";
infoLabel.BringToFront();
this.Controls.Add(infoLabel);
TabIndex and BringToFront I added as an act of desperation, it does not help. BTW the ToolStrip's TabIndex is 2, and its BackColor I changed to transparent.
However, when I placed a label over the ToolStrip in the Designer, it is visible (ie. on top). I analysed the code then but did not see anything different from what I am writing. What am I missing here?
I suggest calling infoLabel.BringToFront(); at the very end, at least after this.Controls.Add(infoLabel); you current code amended:
infoLabel = new Label();
...
infoLabel.Text = "this is info";
// First Add to this
this.Controls.Add(infoLabel);
// Only then we can make infoLabel be the topmost
// among all existing controls which are on this
infoLabel.BringToFront();
We create infoLabel, add it to this and finally make it topmost on this. To make code more readable I suggest something like this:
// Create a label on this
infoLabel = new Label() {
AutoSize = true,
Location = new System.Drawing.Point(200, 10),
Size = new System.Drawing.Size(35, 13),
BackColor = System.Drawing.SystemColors.Control,
Font = new System.Drawing.Font("Arial", 13),
ForeColor = System.Drawing.Color.Black,
TabIndex = 1,
Text = "this is info",
Parent = this // <- instead of this.Controls.Add(infoLabel);
};
// make infoLabel topmost among all controls on this
infoLabel.BringToFront();
Windows Forms controls do not have a property which you can use to set z-index of controls like one can do in CSS.
You'll need to call Parent.SetChildIndex(control, 0);. The control at the front of Controls collection is the topmost in z-order for a container control.

Bold Text in GroupBox

In the following code-snippet I'm creating a GroupBox, where I would like the title to be bold.
Control containerControl;
containerControl = new GroupBox { Name = "" + viewGroupID, Text = viewName, Dock = DockStyle.Top, Height = 20, Padding = new Padding( 2 ) };
The title is being set by the Text Property, but at the moment I have not been able to make it bold.
In the above picture, it's the "View Group A" that needs to be bold.
I have read, that this can be done by using a label, but is there any possible way to to achieve this just by creating a GroupBox?
You can do that by making a new font with FontStyle parameter added:
GroupBox gb = new GroupBox()
{
Font = new Font( DefaultFont.FontFamily, DefaultFont.Size, FontStyle.Bold ),
Text = "Text"
//You can also use 'this.Font' instead of 'DefaultFont' to use the font of your form
};

why Label on Xamarin.forms does not display all text?

I am trying to display almost 300 lines on one page using label control, but i only showing first top lines, i am using the following code, how i can display all lines, is label the appropriate controls?
Label testt = new Label { Text = MainPage.part1, VerticalOptions = LayoutOptions.CenterAndExpand, HorizontalOptions = LayoutOptions.CenterAndExpand, FontAttributes = FontAttributes.Bold, FontSize = 24, TextColor = Color.White };
StackLayout stacklayout = new StackLayout
{
Children = {
TitleOfBook,
testt
}
};
ScrollView scroll = new ScrollView
{
Content = stacklayout
,VerticalOptions=LayoutOptions.FillAndExpand,
HorizontalOptions=LayoutOptions.FillAndExpand
};
Content = scroll;
I came across a similar problem a while back. I believe the issue was that the label has a limit of 100 lines (or something along those lines). You can create a custom label and set the line limit to be higher.
Here is a link to the Xamarin Forum topic which may be able to give you an idea of implementation.

WPF - How to Center a label's Content inside the label

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

Any idea why my usercontrol isn't centering in my tabcontrol?

I'm a little confused as to why this isn't working, since I had it working in a prototype and the only big difference I think is that I use a custom TabItem and UserControl instead of the default ones. I'm trying to get the usercontrol that appears to be centered in the tab window, but it seems to be aligned left.
You hand this method the usercontrol you want to use and it formats it and sticks it in the tabcontrol. In a test solution I did of this earlier, setting scroll's horizontal and vertical alignment to stretch fixed this, but it's not working in this case. Is there some other setting or something else somewhere that would override this?
public void CreateNewTab(UserControlGeneric new_user_control, string tab_header)
{
//TabItem tab = new TabItem();
TabItemIndexed tab = new TabItemIndexed();
//The scrollviewer is created/setup to make sure the usercontrol gets scroll bars if the window if ever made smaller than the usercontrol
ScrollViewer scroll = new ScrollViewer();
//How you programatically set a scrollviewer's height and width to be "Auto"
scroll.Height = Double.NaN;
scroll.Width = Double.NaN;
scroll.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
scroll.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
scroll.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
scroll.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
scroll.Content = new_user_control;
tab.Content = scroll;
tab.Header = tab_header;
//If there aren't any tabs, then hide the "No Workspaces Open" notice (Since we're adding a tab)
if (!tabControl_main.HasItems) label_no_workspaces_open.Visibility = System.Windows.Visibility.Hidden;
tabControl_main.Items.Add(tab);
tabControl_main.SelectedItem = tab;
}

Categories