I want to remove the last row of a grid. I expect the last line to remove the row with the '2' in it. But whether I removeAt row 0 or 1, the 0th row is always removed. I suspect I am missing something subtle about rows vs grids. What am I doing wrong:
// Create row 0
RowDefinition row = new RowDefinition();
row.Height = GridLength.Auto;
myGrid.RowDefinitions.Add(row);
// Add someting to it
TextBox tb = new TextBox();
tb.Text = "1";
Grid.SetColumn(tb, 0);
Grid.SetRow(tb, 0);
myGrid.Children.Add(tb);
// create row 1
row = new RowDefinition();
row.Height = GridLength.Auto;
myGrid.RowDefinitions.Add(row);
// Add something to it
tb = new TextBox();
tb.Text = "2";
Grid.SetColumn(tb, 0);
Grid.SetRow(tb, 1);
myGrid.Children.Add(tb);
// Delete row 1 (second row 0-indexed) <<< only row 0 is deleted
myGrid.RowDefinitions.RemoveAt(1);
Note: I also tried:
myGrid.RowDefinitions.Remove(myGrid.RowDefinitions[1]);
same result. Thanks for any insight.
When you delete a row from a Grid it doesn't remove the children on that row, you will have to do that yourself. You could try iterating over the children of the Grid and remove any elements with a Row value equal to the row you want to remove...
var rowToDelete = 3;
foreach (var element in myGrid.Children)
{
if(Grid.GetRow(element) == rowToDelete)
{
myGrid.Children.Remove(element);
}
}
Related
I have a WPF Grid with 4 rows:
Row 0 : Height = 1.0 (Grid Unit Type Star)
Row 1 - GridSplitter. Height GridLength.Auto
Row 2 - Height Fixed (About 20px)
Row 3 - FlowDocumentScrollViewer: Height GridLength.Auto
Row 1. 2, 3 are initially hidden (for this reasong height of row1 = 1.0). When clicking a button row 1, 2 and 3 are made visible.
I can't drag the splitter to make the flow document scroll viewer smaller.
If I drag the splitter to make it bigger, the Row 2 grows but the row 3 keeps with the same height.
¿How can I get the row 3 growing or shrunking properly? I post my code below:
void BuildComponents()
{
mainGrid= new Grid();
RowDefinition row0 = new RowDefinition();
RowDefinition splitterRow = new RowDefinition();
RowDefinicion fixedHeightRow = new RowDefinition();
RowDefinition scrolldocumentRow= new RowDefinition();
row0.Height = new GridLength(1.0, GridUnitType.Star);
splitterRow.Height = GridLength.Auto;
fixedHeightRow .Height = new GridLength(20, GridUnitType.Pixel);
scrolldocumentRow.Height = GridLength.Auto;
mainGrid.RowDefinitions.Add(row0);
mainGrid.RowDefinitions.Add(splitterRow);
mainGrid.RowDefinitions.Add(fixedHeightRow );
mainGrid.RowDefinitions.Add(scrolldocumentRow);
mainGrid.Children.Add(/*Grid Panel */);
mainGrid.Children.Add(Splitter);
mainGrid.Children.Add(/* Panel */);
mainGrid.Children.Add(/* Flow Document Scroll Viewer */);
Thank you
I Have a Grid (BrkGrid) in ViewMode... I Add dynamic Check Box (brkChkBox) depending on some logic. A foreach is responsible for adding check boxes to a specific row and column of this grid.. But it takes time while adding check boxes to the BrkGrid.. When I Comment the Add Statement (BrkGrid.Children.Add(brkChkBox)) then the code executes faster.. Any help will be highly appreciated...
CheckBox brkChkBox = null;
foreach (var s in this.ViewData.PlnDtShiftBrksDateList)
{
brkChkBox = new CheckBox
{
DataContext = s,
Tag = s.BreakID,
Width = 20,
VerticalAlignment = VerticalAlignment.Top,
};
Binding chkBoxBinding = new Binding("IsSelected");
chkBoxBinding.Source = s;
chkBoxBinding.Mode = BindingMode.TwoWay;
brkChkBox.SetBinding(CheckBox.IsCheckedProperty, chkBoxBinding);
brkChkBox.Click += brkChkBox_Click;
Grid.SetColumn(brkChkBox, gridColDic.FirstOrDefault(x => x.Key == s.BreakID).Value);
Grid.SetRow(brkChkBox, gridRowDic.FirstOrDefault(x => x.Key.Date == s.Date.Date).Value);
BrkGrid.Children.Add(brkChkBox);
}
I'm having trouble binding values to dynamically created controls. When a user loads a custom file and that file will have a unknown number of arguments. Arguments have a Group property that when grouped will dynamically add tabItems to a tabControl. I then loop through the arguments and add a label and, for now, a textbox to a grid inside the tabs. though i intend to use different controls depending on the arument type. I want to bind the argument Value property to the textbox. The tabs, labels and textboxes are added fine but, no value binding
He if my not yet re-factored solution so far;
myTab.Items.Clear();
var args = viewModel.Arguments;
var groups = args.GroupBy(arg => arg.Groups);
foreach (var group in groups)
{
TabItemExt tab = new TabItemExt();
tab.Header = group.Key;
Grid grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
int count = 0;
foreach (var argument in group)
{
RowDefinition newRow = new RowDefinition();
grid.RowDefinitions.Insert(count, newRow);
LabelTextBlock label = new LabelTextBlock();
label.Text = argument.DisplayName;
Grid.SetRow(label, count);
Grid.SetColumn(label, 0);
TextBox textBox = new TextBox();
var binding = new Binding();
binding.Source = viewModel.Arguments[argument.Name];
//binding.Source = argument
binding.Path = new PropertyPath("Value");
textBox.SetBinding(TextBlock.TextProperty, binding);
Grid.SetRow(textBox, count);
Grid.SetColumn(textBox, 1);
grid.Children.Add(label);
grid.Children.Add(textBox);
count += 1;
}
tab.Content = grid;
myTab.Items.Add(tab);
}
textBox.SetBinding(TextBlock.TextProperty, binding);
should have been
textBox.SetBinding(TextBox.TextProperty, binding);
just a little over dependent on intellisense.
I have a grid with two columns and multiple rows, each cell containing a number of controls. Among these controls I have a button which should, when I press it, delete all the controls in the current grid cell. How can I get the index of the grid cell my button is in and how can I delete all controls in this cell?
Does this work for you? you'll need to add a using statement for System.Linq
//get the row and column of the button that was pressed.
var row = (int)myButton.GetValue(Grid.RowProperty);
var col = (int)myButton.GetValue(Grid.ColumnProperty);
//go through each child in the grid.
foreach (var uiElement in myGrid.Children)
{ //if the row and col match, then delete the item.
if (uiElement.GetValue(Grid.ColumnProperty) == col && uiElement.GetValue(Grid.RowProperty) == row)
myGrid.Children.Remove(uiElement);
}
using linq and extending the previous answer, notice the ToList() so you can immediately remove the element
//get the row and column of the button that was pressed.
var row = (int)myButton.GetValue(Grid.RowProperty);
var col = (int)myButton.GetValue(Grid.ColumnProperty);
//go through each child in the grid.
//if the row and col match, then delete the item.
foreach (var uiElement in myGrid.Children.Where(uiElement => (int)uiElement.GetValue(Grid.ColumnProperty) == col && (int)uiElement.GetValue(Grid.RowProperty) == row).ToList())
{
myGrid.Children.Remove(uiElement);
}
I assign a column programmatically to a DataTable like this:
myDataTable.Columns.Add(myDataColumn);
Is there a way to programmatically set the width / size of the column?
ColumnDefinition col1 = new ColumnDefinition();
col1.Width = GridLength.Auto;
ColumnDefinition col2 = new ColumnDefinition();
col2.Width = new GridLength(1,GridUnitType.Star);
grid.ColumnDefinitions.Add(col1);
grid.ColumnDefinitions.Add(col2);
top pieces will auto size columns, bottom piece you can customize size. look into this site for more detail -- http://www.wpftutorial.net/GridLayout.html
This resizes N-1 columns to "Auto" and column N to "Fill"
foreach (var column in dataGrid.Columns)
column.Width = DataGridLength.Auto;
dataGrid.Columns.Last().Width = DataGridLength.SizeToCells;