Issue in creating combobox dynamically - c#

I tried to create a ComboBox dynamically, but cannot create it.
Here is the code that I wrote.
ComboBox com_dynamic = new ComboBox();
com_dynamic.Height = 50;
com_dynamic.Width = 100;
LayoutRoot.Children.Add(com_dynamic);
tb.Margin = new Thickness(0, 145, 87, 0);
tb.VerticalAlignment = VerticalAlignment.Top;
tb.HorizontalAlignment = HorizontalAlignment.Right;
ComboBoxItem com_dynamic_item = new ComboBoxItem();
com_dynamic.AddChild(com_dynamic_item);
com_dynamic_item.Content = "item1";

You should also add the combobox to a container control. If you want to add it in the LayoutRoot, then this is the line you are missing.
LayoutRoot.Children.Add(com_dynamic);
You can reposition the combobox using its margin in the container.

Related

C# - Reordering controls by z-index in Panel

I have a Panel with a PictureBox with Dock = DockStyle.Fill. I need to dynamically add controls to the Panel, but they must stay above the PictureBox.
This is easy within the Designer, but when I do this programmatically, neither SetChildIndex(), BringToFront() or SendToBack() work.
I have to use PictureBox, I can't just set Panel.BackgroundImage because it's glitchy.
I fount this to be an issue with the order of the controls in the panel in the design.cs
Remove the controls from the panel and add them in the correct order.
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Controls.Add(this.button1);
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(260, 238);
this.panel1.TabIndex = 0;
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(260, 238);
this.panel1.TabIndex = 0;
Once you add your dynamic control to the Controls find it and BringToFront like as follows:
TextBox tb = new TextBox
{
Location = new Point(100, 100),
Name = "Textbox1"
};
this.Controls.Add(tb);
var contr = Controls.Find("Textbox1", true)[0];
contr.BringToFront();
Alternatively, once you add new dynamic control. Apply SendToBack to the PictureBox.
pictureBox1.SendToBack();

Accessing TextBlock in a grid with LINQ

I am dynamically generating rows in a XAML grid:
for (int i = 0; i < AssociatedSteps.Length; i++)
{
//if (i == 0 || AssociatedSteps[i].OriginPkg != AssociatedSteps[i-1].OriginPkg)
//{
//Define new Row to add
RowDef = new RowDefinition();
RowDef.Height = new GridLength(60);
//Add row definition to Grid
WorkPackageViewResults.RowDefinitions.Add(RowDef);
//Define the control that will be added to new row
Text1 = new TextBlock();
Text1.Text = AssociatedSteps[i].SF01;
Text1.Width = Settings.workPackageColumn5Width;
Text1.TextAlignment = TextAlignment.Center;
Text2 = new TextBlock();
Text2.Text = AssociatedSteps[i].SF10;
Text2.Width = Settings.workPackageColumn5Width;
Text2.TextAlignment = TextAlignment.Center;
Text3 = new TextBlock();
Text3.Text = AssociatedSteps[i].OriginPkg;
Text3.Width = Settings.workPackageColumn5Width;
Text3.TextAlignment = TextAlignment.Center;
Button1 = new Button();
Button1.Content = AssociatedSteps[i].Description;
Button1.Width = Settings.workPackageColumn5Width;
Button1.Click += new RoutedEventHandler(ProgressUpdateButton_Click);
Button1.Tag = AssociatedSteps[i].StepID;
//create stackpanel and define which row to add the stackpanel to
StackP = new StackPanel();
//StackP.Background = new SolidColorBrush(Colors.Wheat);
StackP.SetValue(Grid.RowProperty, i);
StackP.Orientation = Orientation.Horizontal;
StackP.Margin = new Thickness(0, 0, 0, 0);
PercentComplete = new TextBlock();
PercentComplete.Text = (Convert.ToDouble(AssociatedSteps[i].ToDateQty) / Convert.ToDouble(AssociatedSteps[i].MTOQty)).ToString();
PercentComplete.Width = Settings.workPackageColumn5Width;
PercentComplete.TextAlignment = TextAlignment.Center;
//add your control to the stackpanel
StackP.Children.Add(Text1);
StackP.Children.Add(Text2);
StackP.Children.Add(Text3);
StackP.Children.Add(Button1);
StackP.Children.Add(PercentComplete);
//add the stackpanel to the grid
WorkPackageViewResults.Children.Add(StackP);
}
I'm trying to use LINQ to get access to the grid being programatically updated above:
<Grid Name="WorkPackageViewResults">
</Grid>
I'm not seeing they type of options you would for a cell. I want to to access a particular rows PercentComplete TextBlock so I can update the text. How do I accomplish this?
Since there's only one StackPanel you could say e => e.GetType() == typeof(StackPanel). If you ever have many, then add a Name to the StackPanel and pull it by that.

Click event for programatically generated XAML buttons

I'm programatically generating a table where Text2 the columns are a button:
for (int i = 0; i < workPackages.Length; i++)
{
//Define new Row to add
RowDef = new RowDefinition();
RowDef.Height = new GridLength(60);
//Add row definition to Grid
WorkPackageResults.RowDefinitions.Add(RowDef);
//Define the control that will be added to new row
Text1 = new TextBlock();
Text1.Text = workPackages[i].EWPStatus;
Text1.Width = 100;
Text1.TextAlignment = TextAlignment.Center;
Text2 = new Button();
Text2.Content = workPackages[i].EWPCode;
Text2.Width = 300;
Text3 = new TextBlock();
Text3.Text = workPackages[i].Description;
Text3.Width = 500;
Text3.TextAlignment = TextAlignment.Center;
Text4 = new TextBlock();
Text4.Text = workPackages[i].ForeBadge;
Text4.Width = 100;
Text4.TextAlignment = TextAlignment.Center;
//create stackpanel and define which row to add the stackpanel to
StackP = new StackPanel();
StackP.SetValue(Grid.RowProperty, i);
StackP.Orientation = Orientation.Horizontal;
StackP.Margin = new Thickness(50, 0, 0, 0);
//add your control to the stackpanel
StackP.Children.Add(Text1);
StackP.Children.Add(Text2);
StackP.Children.Add(Text3);
StackP.Children.Add(Text4);
//add the stackpanel to the grid
WorkPackageResults.Children.Add(StackP);
}
How do I programatically add a click event? And when that click event is executed how do I know which button it came from?
Add an event handler to Text2.Click that will pass the sender as a parameter, that's how you know which one was clicked.

How to set ScrollViewer or scroll (dynamically created grid)?

Here I'm dynamically creating grid on form. The code is working fine but I want this grid in scroll viewer or scroll bar (vertical). Can any one tell me how to set scroll in this code.
Grid DynamicGrid = new Grid();
DynamicGrid.Width = 400;
DynamicGrid.HorizontalAlignment = HorizontalAlignment.Right;
DynamicGrid.VerticalAlignment = VerticalAlignment.Top;
DynamicGrid.Margin = new Thickness(50);
DynamicGrid.ShowGridLines = false;
DynamicGrid.Background = new SolidColorBrush(Colors.LightSteelBlue);
// Create Columns
ColumnDefinition gridCol1 = new ColumnDefinition();
DynamicGrid.ColumnDefinitions.Add(gridCol1);
// Create Rows
RowDefinition gridRow1 = new RowDefinition();
gridRow1.Height = new GridLength(30);
DynamicGrid.RowDefinitions.Add(gridRow1);
TextBlock txtBlock2 = new TextBlock();
txtBlock2.Text = "Age";
txtBlock2.FontSize = 14;
txtBlock2.FontWeight = FontWeights.Bold;
txtBlock2.Foreground = new SolidColorBrush(Colors.Green);
txtBlock2.VerticalAlignment = VerticalAlignment.Top;
Grid.SetRow(txtBlock2, 0);
Grid.SetColumn(txtBlock2, 1);
TextBlock ageText = new TextBlock();
ageText.Text = "33";
ageText.FontSize = 12;
ageText.FontWeight = FontWeights.Bold;
Grid.SetRow(ageText, 1);
Grid.SetColumn(ageText, 1);
// Display grid into a Window
window.Content = DynamicGrid;
I think the easiest way to do that is to include the Grid into a ScrollViewer:
ScrollViewer viewer = new ScrollViewer();
viewer.Content = DynamicGrid;
Window.Content = viewer;
Best regards,

Add Controls to GroupBox which was created dynamically

I add GroupBoxes to an ItemsControl dynamically using:
string name_ = "TestName", header_ = "TestHeader"
GroupBox MyGroupBox = new GroupBox { Name = name_, Header= header_, Width = 240, Height = 150, Foreground=new SolidColorBrush(Color.FromArgb(255, 0, 0, 0)) };
MyItemsControl.Items.Add(MyGroupBox);
Now I need to add content to this GroupBox, like a few TextBlocks created like:
TextBlock MyTextBlock = new TextBlock {Text = "test"};
But I can't figure out how to do that. Normally to a Grid or something like that I would just use .Children.Add(MyTextBlock), but that doesn't work here.
Also I have to be able to remove specific Items from the ItemsControl again (best would be by the name of the Item, name_ in this example).
Try something like that
GroupBox groupBox1 = new GroupBox();
Grid grid1 = new Grid();
TextBlock MyTextBlock = new TextBlock {Text = "test"};
groupBox1.Width = 185;
groupBox1.Height = 160;
grid1.Height = 185;
grid1.Width = 160;
grid1.Children.Add(MyTextBlock);
groupBox1.Content = grid1;
mainWindow.canvas.Children.Add(groupBox1);
The GroupBox has only a Content Property which is designed to hold a ContentPresentor. You can add a Grid/Canvas etc.. to the GroupBox and then add your content to that.

Categories

Resources