I am creating a Button control using C# as mentioned below in the code. I have created the rounded border for the button style. I am not able to see any property to assign the Border in the button.
var button = new System.Windows.Controls.Button
{
Name = "BtnOk",
Content = "OK",
Height = 20,
Width = 60,
HorizontalAlignment = HorizontalAlignment.Center,
Background = Brushes.DarkGray,
Foreground = Brushes.WhiteSmoke,
Margin = new Thickness(0,0,0,5)
};
Border border = new Border();
border.CornerRadius = new CornerRadius(3);
How can I apply Border in button programatically?
a button cannot aplly a border. a border can decorate a button:
border.Child = button;
usually Buttons already have a Border inside their Template (ControlTemplate). that Border isn't easily accessible - there is no special property of Button class, but that border can be found in visual tree after template was loaded.
additionally that Border can be customized by default style if you put it in Button.Resources. change CorderRadius using Style.Setter:
var button = new System.Windows.Controls.Button
{
Name = "BtnOk",
Content = "OK",
Height = 20,
Width = 60,
HorizontalAlignment = HorizontalAlignment.Center,
Background = Brushes.DarkGray,
Foreground = Brushes.WhiteSmoke,
Margin = new Thickness(0, 0, 0, 5)
};
var style = new Style
{
TargetType = typeof(Border),
Setters = { new Setter { Property = Border.CornerRadiusProperty, Value = new CornerRadius(3) } }
};
button.Resources.Add(style.TargetType, style);
or using object/collection initializers:
var button = new System.Windows.Controls.Button
{
Name = "BtnOk",
Content = "OK",
Height = 20,
Width = 60,
HorizontalAlignment = HorizontalAlignment.Center,
Background = Brushes.DarkGray,
Foreground = Brushes.WhiteSmoke,
Margin = new Thickness(0, 0, 0, 5),
Resources =
{
{
typeof(Border), new Style
{
TargetType = typeof(Border),
Setters =
{
new Setter { Property = Border.CornerRadiusProperty, Value = new CornerRadius(13) }
}
}
}
}
};
if many buttons should have different CornerRadius, changing Button's Template can be a solution. Change template and set CornerRadius as attached dependency property, like shown in this post: Set a property of a nested element in an WPF style
There is indeed a Border element in the default ControlTemplate for the Button but the easiest way to set the CornerRadius property of it, without having to define a custom template, is to wait until the Button has been loaded and then get a reference to it. Try this:
var button = new System.Windows.Controls.Button
{
Name = "BtnOk",
Content = "OK",
Height = 20,
Width = 60,
HorizontalAlignment = HorizontalAlignment.Center,
Background = Brushes.DarkGray,
Foreground = Brushes.WhiteSmoke,
Margin = new Thickness(0, 0, 0, 5)
};
button.Loaded += (ss, ee) =>
{
Border border = button.Template.FindName("border", button) as Border;
if (border != null)
border.CornerRadius = new CornerRadius(3);
};
Related
From XAML i'm created Popup, but i need create him from code.
My code:
var popup = new Popup()
{
IsVisible = true,
IsOpen = true,
PlacementRect = new(50, 50, 100, 75),
Child = new Border {
BorderThickness = new(0.75),
BorderBrush = Brushes.Black,
Padding = new(6, 3),
Child = new TextBlock
{
Background = Brushes.White,
Text = textContent
}
}
};
I have a TableLayoutPanel that contains a Label and a ComboBox:
var lblDesignGroup = new Label
{
Name = "lblDesignGroup",
Text = "DesignGroup",
Margin = new Padding(0, 50, 0, 0)
};
tlpCallLog.Controls.Add(lblDesignGroup, 0, 0);
var cboDesignGroup = new ComboBox
{
Name = "cboDesignGroup",
DataSource = designGroups,
DisplayMember = "DesignGroupName",
ValueMember = "DesignGroupId",
Margin = new Padding(0, 50, 0, 0),
DropDownStyle = ComboBoxStyle.DropDownList
};
Once I create them, I setup the TableLayoutPanel as:
tlpCallLog.ColumnStyles.Clear();
for (int i = 0; i < tlpCallLog.ColumnCount; i++)
{
tlpCallLog.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
}
tlpCallLog.RowStyles.Clear();
for (int i = 0; i < tlpCallLog.RowCount; i++)
{
tlpCallLog.RowStyles.Add(new RowStyle(SizeType.AutoSize));
}
tlpCallLog.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
But for some reason, I have blank space between the Label and the DropDown.
As you can see, the DropDown width adjusts correctly, it does not have any free space, but the Label has it.
I want to remove that space. How can I achieve that?
The Label Control auto-sizes in a default layout (when child of a Form, as a standard scenario, since the Form performs its layout when initialized). When created like this, the AutoSize property is set to true because that's the default value, but the Layout is never performed.
When you explicitly set AutoSize = true, then the layout is performed.
Set both the TableLayoutPanel and the Label to AutoSize:
tlpCallLog.AutoSize = true;
tlpCallLog.RowStyles.Clear();
//[...]
var lblDesignGroup = new Label {
AutoSize = true,
Name = "lblDesignGroup",
Text = "DesignGroup",
Margin = new Padding(0, 53, 0, 0)
};
tlpCallLog.Controls.Add(lblDesignGroup, 0, 0);
var cboDesignGroup = new ComboBox {
//[...]
}
I suggest to set the Label's Margin = new Padding(0, 53, 0, 0): 3 pixels down, to align with the ComboBox text. It's a valid measure with (almost) any Font size.
Another suggestion is to set the [Form].AutoScaleMode = AutoScaleMode.Dpi.
I have MainForm parent mdi window which conatan Menu, ToolBar and StatusStrip created from designer in Visual Studio.
In that MainForm am showing child forms which content was created programmaticly (panels, textboxes, labels, grids etc...)
I have problem when i create DataGridView and set Dock.Top, Dock.Fill my grid is hidden behind parent toolbar.
I can't use Document Outline on programmaticlly created controls.
I try to use BringToFront but again i have the some problem.
To understand better check my screenhsot.
private void InitializeDefaultObjects()
{
/// SPLIT CONTAINER
_splitContainer = new SplitContainer()
{
SplitterDistance = 270,
Panel1MinSize = 270,
Margin = new Padding(15, 15, 15, 15),
Dock = DockStyle.Fill,
BackColor = Color.Gainsboro,
BorderStyle = BorderStyle.None,
Orientation = Orientation.Vertical,
SplitterWidth = 4,
FixedPanel = FixedPanel.Panel1,
RightToLeft = RightToLeft.No,
IsSplitterFixed = false,
Panel1Collapsed = false,
};
_splitContainer.Panel1.Padding = new Padding(5, 5, 5, 5);
_splitContainer.BringToFront();
this.Controls.Add(this._splitContainer);
/// TOOLSTRIP IN SPLIT CONTAINER 1
/// This toolstrip is merged with parent tolstrip
_toolStrip = new System.Windows.Forms.ToolStrip();
_toolStrip.Height = 100;
_toolStrip.Dock = DockStyle.Bottom;
_toolStrip.Visible = false;
_toolStrip.SendToBack();
_toolStrip.BringToFront();
// Toolstrip title
_toolStripTitleLabel = new ToolStripLabel()
{
Text = this.Text,
Alignment = ToolStripItemAlignment.Left,
Font = new Font("Segoe UI", 10F, System.Drawing.FontStyle.Bold),
Image = this.ToolStripTitleImage,
ImageAlign = ContentAlignment.MiddleCenter,
ImageScaling = ToolStripItemImageScaling.SizeToFit
};
// separatar
ToolStripSeparator toolStripSeparator = new ToolStripSeparator() { Alignment = ToolStripItemAlignment.Right };
// Close button
ToolStripButton closeToolStripButton = new ToolStripButton()
{
Text = "Zatvori",
Alignment = ToolStripItemAlignment.Right,
Image = DFMSoftware.Core.Properties.Resource.Close_icon,
TextImageRelation = TextImageRelation.ImageAboveText
};
// Close button event
closeToolStripButton.Click += new EventHandler(closeToolStripButton_Click);
_toolStrip.Items.Add(_toolStripTitleLabel);
_toolStrip.Items.Add(closeToolStripButton);
_toolStrip.Items.Add(toolStripSeparator);
// add toolstripe to splitpanel
this._splitContainer.Panel2.Controls.Add(_toolStrip);
// datagridview
_dataGridView = new System.Windows.Forms.DataGridView();
_dataGridView.Dock = Dock.Fill;
// add grid to splitpanel 2
_splitContainer.Panel2.Controls.Add(_dataGridView);
}
Here in code you can see.
1. I create SplitContainer and set BringToFront() and set Dock.Fill
2. In SplitContainer in Panel2 i add DataGridView with Dock.Fill and again is behind parent.
Am not shure how to slove this does anyone know? THanks
Screensho1
I'm pretty noob with UWP stuff. I'm trying to create dynamic hamburger menu.
I was able to create PrimaryButtons element, and binding it in XAML worked as espected:
var loginButton = new HamburgerButtonInfo();
loginButton.ClearHistory = true;
loginButton.PageParameter = "";
loginButton.PageType = typeof(Views.Login);
var stackPanel = new StackPanel { Orientation = Orientation.Horizontal };
stackPanel.Children.Add(new SymbolIcon { Symbol = Symbol.Contact, Width = 48, Height = 48 });
stackPanel.Children.Add(new TextBlock { Text = "Login", VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(12, 0, 0, 0) });
loginButton.Content = stackPanel;
But I'd like to have a cleaner solution, so I tried to extend HamburgerButtonInfo class:
class MenuItem : HamburgerButtonInfo
{
private Symbol symbol;
private String text;
StackPanel stackpanel = new StackPanel { Orientation = Orientation.Horizontal };
TextBox textbox = new TextBox { VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(12, 0, 0, 0) };
SymbolIcon symbolicon = new SymbolIcon { Width = 48, Height = 48 };
public MenuItem():base()
{
StackPanel.Children.Add(symbolicon);
StackPanel.Children.Add(textbox);
this.Content = StackPanel;
}
public String Text
{
get { return text; }
set {
textbox.Text = value;
Set(ref text, value);
}
}
public StackPanel StackPanel
{
get { return stackpanel; }
}
public Symbol Symbol
{
get { return symbol; }
set {
symbolicon.Symbol = value;
Set(ref symbol, value);
}
}
}
Putting it all together, I expected to get the same result:
PrimaryButtons.Add(loginButton);
PrimaryButtons.Add(new MenuItem() { PageType=typeof(Views.Login), PageParameter="", ClearHistory=true, Text="Login", Symbol=Symbol.Contact });
But here's the result
Am I missing something? Is that the right approach for this scenario?
Can it be done? Absolutely.
var stackPanel = new StackPanel
{
Orientation = Orientation.Horizontal
};
stackPanel.Children.Add(new SymbolIcon
{
Width = 48,
Height = 48,
Symbol = Symbol.UnSyncFolder
});
stackPanel.Children.Add(new TextBlock
{
Margin = new Thickness(12, 0, 0, 0),
VerticalAlignment = VerticalAlignment.Center,
Text = "UnSync Folder"
});
var button = new HamburgerButtonInfo
{
Content = stackPanel,
ButtonType = HamburgerButtonInfo.ButtonTypes.Toggle,
ClearHistory = false,
PageType = typeof(Views.DetailPage)
};
MyHamburgerMenu.PrimaryButtons.Add(button);
Looks like this (I tried it in the Search sample).
It's more verbose because the XAML syntax is so compact, but you can do it in code-behind if you want to. You might just want to change visibility of an existing button if that is an option.
Best of luck!
I am creating Label, Textbox and a button dynamically. I need Button to appear in the same line as textbox to its right.
This is the code i am using:
Label lbl = new Label()
{
Content = "Some Label",
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
Height = 28,
};
TextBox tb = new TextBox()
{
Text = "Some Text",
IsReadOnly = true,
};
Button btn = new Button()
{
Content = "Click Me",
HorizontalAlignment = HorizontalAlignment.Left
Margin = new Thickness(tb.ActualWidth),
};
I am assigning Button Margin to the Right of TextBox but it still appears in the next line under the textbox.
What am i doing wrong here?
You can use StackPanel to solve your problem:
StackPanel spMain = new StackPanel() { Orientation = Orientation.Vertical };
Label lbl = new Label()
{
Content = "Some Label",
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
Height = 28,
};
StackPanel spInner = new StackPanel() { Orientation = Orientation.Horizontal };
TextBox tb = new TextBox()
{
Text = "Some Text",
IsReadOnly = true,
};
Button btn = new Button()
{
Content = "Click Me",
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(tb.ActualWidth),
};
spInner.Children.Add(tb);
spInner.Children.Add(btn);
spMain.Children.Add(lbl);
spMain.Children.Add(spInner);
You can check following link for more information:
http://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel.orientation.aspx
It depends on what the page content that you are placing the controls on is, is it a grid or something else?
Why not also create a stackpanel which will properly contain your items in the fashion as needed.
Why don't you place all the created controls in a StackPanel with its Orientation set to Horizontal. This way they will always placed next to eachother.
var stPanel = new StackPanel { Orientation = Orientation.Horizontal };
var button = new Button() { ... }
stPanel.Children.Add(button);
//And so on
Edit: kmatyaszek was ahead of me... :)
i think it would be better if you use the Content methode from the instance of Button
private byte _count;
internal void FillbtnSubCat(Grid grid)
{
var myDefinition = new ColumnDefinition();
var myButton = new Button();
var myBlock = new TextBlock()
{
TextAlignment = TextAlignment.Center,
TextWrapping = TextWrapping.Wrap,
Text = "Some Text",
Margin = new Thickness(5, 10, 5, 10)
};
Grid.SetColumn(myButton, _count);
myButton.Margin = new Thickness(5, 10, 5, 25);
myButton.MinWidth = 30;
myButton.Content = myBlock;
myDefinition.Width = new GridLength(68);
grid.ColumnDefinitions.Add(myDefinition);
grid.Children.Add(myButton);
_count++;
}
XAML
<Grid Name="Grid1" Height="100" Width="auto">
</Grid>