I'm working on a WPF App, and I would like to create the entire window from c# code, instead of from XML.
I tried the following code, but nothing happens, the grid is not displayed. Did I miss something? Is it possible to do it this way or is there any other solution?
public MainWindow()
{
Grid grd = new Grid();
grd.Margin = new System.Windows.Thickness(10, 10, 10, 0);
grd.Background = new SolidColorBrush(Colors.White);
grd.Height = 104;
grd.VerticalAlignment = System.Windows.VerticalAlignment.Top;
grd.ColumnDefinitions.Add(new ColumnDefinition());
grd.ColumnDefinitions.Add(new ColumnDefinition());
grd.ColumnDefinitions.Add(new ColumnDefinition());
RowDefinition row = new RowDefinition();
row.Height = new System.Windows.GridLength(45);
grd.RowDefinitions.Add(row);
row = new RowDefinition();
row.Height = new System.Windows.GridLength(45);
grd.RowDefinitions.Add(row);
row = new RowDefinition();
row.Height = new System.Windows.GridLength(45);
grd.RowDefinitions.Add(row);
InitializeComponent();
}
Grid grd was created, but not added to Window.
InitializeComponent();
this.Content = grd;
it will replace all content which was declared in XAML (if any).
However, Grid is a Panel and doesn' have visual representation itself, so window with Grid without child element will still look empty. Try grd.ShowGridLines = true; to see rows and columns
Grid documentation shows a large example actually, with equivalent c# code and xaml markup
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.
Excuse me, a quick question:
I am using this routine to display a list of strings into a datagridview:
List<string> MyDataSource = MyList.Select(x => Path.GetFileNameWithoutExtension(x)).ToList();
AllVideosGrid.DataSource = MyDataSource.ConvertAll(x => new { Value = x });
Designer generated code:
// MyGrid
//
this.AllVideosGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.AllVideosGrid.Location = new System.Drawing.Point(33, 185);
this.AllVideosGrid.Name = "AllVideosGrid";
this.AllVideosGrid.RowTemplate.Height = 24;
this.AllVideosGrid.Size = new System.Drawing.Size(319, 498);
this.AllVideosGrid.TabIndex = 32;
And it gets displayed like this:
I would like rows to be resized to the datagridview size that I set initially in my designer. How can I do that?
for horizontal strecth use this:
this.AllVideosGrid.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
For vertical I think that's not possible, eventually you can set the height for each row
Hi i need to draw an checkbox within the rectangle.is there any predefined method in c# WPF .or how to i acheive this senario in WPF.
You may try this:
CheckBox checkbox = new CheckBox();
checkbox.Content = "Content";
checkbox.Height = 50;
checkbox.Width = 100;
checkbox.IsChecked = true;
checkbox.HorizontalAlignment = HorizontalAlignment.Left;
VisualBrush vb = new VisualBrush(checkbox);
drawingContext.DrawRectangle(vb, null, new Rect(50, 50, 100, 50));
1.) Create the Rectanle
2.) Create the checkbox
3.) use rectangle.Controls.Add(checkbox)
Hope this helps :)
I want a grid layout for displaying search results. The grids should have headlines Beneficial Owner, Commercial Operator and Registered Owner. The results should then be displayed under the right headline.
I am trying to achieve this layout:
But this is what i get:
My C#/SilverLight code:
// Create a 3 column grid
StackPanel deptStackPanel = new StackPanel();
deptStackPanel.Margin = new Thickness(10);
stackPanelSearchResults.Children.Add(deptStackPanel);
Grid.SetColumn(deptStackPanel, 3);
Grid.SetRow(deptStackPanel, 3);
// Add headlines for theese columns
TextBlock deptListHeadingBeneficialOwner = new TextBlock();
deptListHeadingBeneficialOwner.Text = "Beneficial Owner";
TextBlock deptListHeadingCommercialOperator = new TextBlock();
deptListHeadingCommercialOperator.Text = "Commercial Operator";
TextBlock deptListHeadingRegisteredOwnerName = new TextBlock();
deptListHeadingRegisteredOwnerName.Text = "Registered Owner";
deptStackPanel.Children.Add(deptListHeadingBeneficialOwner);
deptStackPanel.Children.Add(deptListHeadingCommercialOperator);
deptStackPanel.Children.Add(deptListHeadingRegisteredOwnerName);
Grid.SetColumn(deptListHeadingBeneficialOwner, 0);
Grid.SetColumn(deptListHeadingCommercialOperator, 1);
Grid.SetColumn(deptListHeadingRegisteredOwnerName, 2);
Add three grids inside the layout grid from your xaml file or from the back end. Put a stack panel inside that and then add the text blocks for your data. You are just adding text blocks one below the other.
I found the solution my self:
// Create the Grid
Grid myGrid = new Grid();
myGrid.Width = 400;
myGrid.Margin = new Thickness(9, 0, 0, 0);
myGrid.HorizontalAlignment = HorizontalAlignment.Left;
myGrid.VerticalAlignment = VerticalAlignment.Top;
myGrid.ShowGridLines = true;
// Define the Columns
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
myGrid.ColumnDefinitions.Add(colDef1);
myGrid.ColumnDefinitions.Add(colDef2);
myGrid.ColumnDefinitions.Add(colDef3);
// Define the Rows
RowDefinition rowDef1 = new RowDefinition();
RowDefinition rowDef2 = new RowDefinition();
RowDefinition rowDef3 = new RowDefinition();
RowDefinition rowDef4 = new RowDefinition();
myGrid.RowDefinitions.Add(rowDef1);
myGrid.RowDefinitions.Add(rowDef2);
myGrid.RowDefinitions.Add(rowDef3);
myGrid.RowDefinitions.Add(rowDef4);
// Add the second text cell to the Grid
TextBlock txtBeneficialOwner = new TextBlock();
txtBeneficialOwner.Text = "Beneficial Owner";
txtBeneficialOwner.FontWeight = FontWeights.Bold;
Grid.SetRow(txtBeneficialOwner, 0);
Grid.SetColumn(txtBeneficialOwner, 0);
// Add the third text cell to the Grid
TextBlock txtCommercialOperator = new TextBlock();
txtCommercialOperator.Text = "Commercial Operator";
txtCommercialOperator.FontWeight = FontWeights.Bold;
txtCommercialOperator.Margin = new Thickness(9, 0, 0, 4);
Grid.SetRow(txtCommercialOperator, 0);
Grid.SetColumn(txtCommercialOperator, 1);
// Add the fourth text cell to the Grid
TextBlock txtRegisteredOwnerName = new TextBlock();
txtRegisteredOwnerName.Text = "Registered Owner";
txtRegisteredOwnerName.FontWeight = FontWeights.Bold;
txtRegisteredOwnerName.Margin = new Thickness(9, 0, 0, 4);
Grid.SetRow(txtRegisteredOwnerName, 0);
Grid.SetColumn(txtRegisteredOwnerName, 2);
// Add the TextBlock elements to the Grid Children collection
myGrid.Children.Add(txtBeneficialOwner);
myGrid.Children.Add(txtCommercialOperator);
myGrid.Children.Add(txtRegisteredOwnerName);
stackPanelSearchResults.Children.Add(myGrid);
I am creating the DataGridTemplateColumn dynamically.
var binding = new Binding
{
Path = new PropertyPath("MyProperty"),
UpdateSourceTrigger = UpdateSourceTrigger.LostFocus
};
var converterParameter = new List<object> { header, rows, myGrid };
binding.Converter = new MyConverter();
binding.ConverterParameter = converterParameter;
var textBoxValue = new FrameworkElementFactory(typeof(TextBox));
totalUnitsValue.SetBinding(TextBox.TextProperty, binding);
totalUnitsValue.SetValue(TextBox.HorizontalContentAlignmentProperty, HorizontalAlignment.Right);
totalUnitsValue.SetValue(TextBox.WidthProperty, 40.0);
totalUnitsValue.SetValue(TextBox.MarginProperty, new Thickness(4, 0, 10, 0));
var factoryElement = new FrameworkElementFactory(typeof(StackPanel));
factoryElement.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
factoryElement.AppendChild(textBoxValue );
var column = new DataGridTemplateColumn
{
Header = header,
CellTemplate = new DataTemplate { VisualTree = factoryElement }
};
myGrid.Columns.Add(column);
This works fine for few columns. But if i create 10 or more columns (80 -90) textBoxes then the last created TextBoxes do not allow me to change the value or do not allow me to put focus on the TextBox. It becomes like TextBlock.
EDIT:
IF I REMOVE THE STACKPANEL, THEN THERE IS NO ISSUE WITH THE TEXTBOX BUT I NEED TO SHOW MORE THAN ONE ELEMENT, SO I NEED TO HAVE SOME SORT OF CONTAINER.ANY HELP ON THAT.
Please guide what could be tghe