Creating a resizable grid in WPF - wpf

I need to write a C# WPF program in order to let the user individually modify the width and height of a grid using the mouse. After some reading, I've found out that WPF featues the GridSplitter control, which seems to be a possible solution for my problem. So far, this is my approach:
private const int NumCols = 5;
private const int NumRows = 7;
private void CreateDynamicWPFGrid()
{
// Create the Grid
var dynamicGrid = new Grid();
for (int i = 0; i < NumCols - 1; ++i )
{
// Define 2 * (NumCols - 1) columns. For every two columns, the first one will hold a label
// whereas the second one will hold a vertical splitter.
var gridColDefA = new ColumnDefinition();
// The gridColDefB is for the splitter.
var gridColDefB = new ColumnDefinition();
gridColDefB.Width = new GridLength(1, GridUnitType.Auto);
dynamicGrid.ColumnDefinitions.Add(gridColDefA);
dynamicGrid.ColumnDefinitions.Add(gridColDefB);
}
{
// The last column only needs a cell for holding a label. No splitter whatsoever.
var gridColDef = new ColumnDefinition();
dynamicGrid.ColumnDefinitions.Add(gridColDef);
}
for (int j = 0; j < NumRows - 1; ++j)
{
var gridRowDefA = new RowDefinition();
var gridRowDefB = new RowDefinition();
// The gridRowDefB is for the splitter.
gridRowDefB.Height = new GridLength(1, GridUnitType.Auto);
dynamicGrid.RowDefinitions.Add(gridRowDefA);
dynamicGrid.RowDefinitions.Add(gridRowDefB);
}
{
// The last row only needs a cell for holding a label. No splitter whatsoever.
var gridRowDef = new RowDefinition();
dynamicGrid.RowDefinitions.Add(gridRowDef);
}
for (int i = 0; i < NumCols - 1; ++i )
{
for(int j = 0; j < NumRows - 1; ++j )
{
// Insert the label.
var label = new Label();
label.Content = "C" + i + "-R" + j;
label.Background = new SolidColorBrush(Colors.Azure);
Grid.SetColumn(label, 2 * i);
Grid.SetRow(label, 2 * j);
dynamicGrid.Children.Add(label);
// Insert the horizontal splitter.
var horizontalGridSplitter = new GridSplitter();
horizontalGridSplitter.Height = 1;
horizontalGridSplitter.Background = new SolidColorBrush(Colors.DarkSlateBlue);
horizontalGridSplitter.HorizontalAlignment = HorizontalAlignment.Stretch;
horizontalGridSplitter.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(horizontalGridSplitter, 2 * i );
Grid.SetRow(horizontalGridSplitter, 2 * j + 1);
Grid.SetRowSpan(horizontalGridSplitter, 1);
Grid.SetColumnSpan(horizontalGridSplitter, 1);
dynamicGrid.Children.Add(horizontalGridSplitter);
// Insert the vertical splitter.
var verticalGridSplitter = new GridSplitter();
verticalGridSplitter.Width = 1;
verticalGridSplitter.Background = new SolidColorBrush(Colors.DarkSlateBlue);
verticalGridSplitter.HorizontalAlignment = HorizontalAlignment.Center;
verticalGridSplitter.VerticalAlignment = VerticalAlignment.Stretch;
Grid.SetColumn(verticalGridSplitter, 2 * i + 1);
Grid.SetRow(verticalGridSplitter, 2 * j + 1);
Grid.SetRowSpan(verticalGridSplitter, 1);
Grid.SetColumnSpan(verticalGridSplitter, 1);
dynamicGrid.Children.Add(verticalGridSplitter);
}
}
// Display grid into a Window
Content = dynamicGrid;
}
The output I'm getting is as follows:
Notice that I'm only able to resize the rows (don't know why the vertical splitters don't show up) and, for some reason, when I grab a horizontal splitter it resizes the whole row and not just the individual cell. Any ideas? Please, See the following screenshot to see the resizing in action:
This is what I'd expect if I resize cell (0,0) (the image has being manually edited by me):
Thanks in advance!

If you remove the line setting the row for your verticalGridSplitter and set them to span NumRows, you will see your vertical splitters. But ultimately, I think you are trying to do something that the grid with splitters can't do. You can't resize the width and height of individual cells, only whole rows and columns.
After all, if you make C0-R0 taller, what do you expect the rest of that row to do?

Related

Keep tablelayoputpanel rows height identical as rows are added dynamically

I have a TableLayoutPanel whose rows are set to autoresize. It has two columns, but rows can be added dynamically at run time.
How do I add a row, and then have the height of each roenter code herew be the same percentage? I tried this code after adding the control:
private void BindRawData(int rc) // row number is passed in
{
var percent = 100 - (tableLayoutPanel1.RowCount * 10);
tableLayoutPanel2.Controls.Add(grid1, 1, rc);
for(int i = 0; i < tableLayoutPanel2.RowStyles.Count - 1; i++)
{
var panel = tableLayoutPanel2.RowStyles[i];
panel.SizeType = SizeType.AutoSize;
}
}
The result is a grid on top that is really tiny, and the next grid takes up almost the whole panel.

Show certain number of datapoints on chart at a time

I'm new to charting in winforms and I'm having a little trouble getting the chart to display a set number of points at time.
Using the code from this answer https://stackoverflow.com/a/5146430 :
private void FillChart()
{
int blockSize = 100;
// generates random data (i.e. 30 * blockSize random numbers)
Random rand = new Random();
var valuesArray = Enumerable.Range(0, blockSize * 30).Select(x => rand.Next(1, 10)).ToArray();
// clear the chart
chart1.Series.Clear();
// fill the chart
var series = chart1.Series.Add("My Series");
series.ChartType = SeriesChartType.Line;
series.XValueType = ChartValueType.Int32;
for (int i = 0; i < valuesArray.Length; i++)
series.Points.AddXY(i, valuesArray[i]);
var chartArea = chart1.ChartAreas[series.ChartArea];
// set view range to [0,max]
chartArea.AxisX.Minimum = 0;
chartArea.AxisX.Maximum = valuesArray.Length;
// enable autoscroll
chartArea.CursorX.AutoScroll = true;
// let's zoom to [0,blockSize] (e.g. [0,100])
chartArea.AxisX.ScaleView.Zoomable = true;
chartArea.AxisX.ScaleView.SizeType = DateTimeIntervalType.Number;
int position = 0;
int size = blockSize;
chartArea.AxisX.ScaleView.Zoom(position, size);
// disable zoom-reset button (only scrollbar's arrows are available)
chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
// set scrollbar small change to blockSize (e.g. 100)
chartArea.AxisX.ScaleView.SmallScrollSize = blockSize;
}
What would I change here to only show, say three data points at a time? I tried changing the Maximum of the x-axis, which worked but took away the scrolling bar. I also tried messing around with the blockSize variable here.

AS3 - Best method for dynamically creating a series of text fields?

I am (successfully) creating a column of boxes via a loop, the meat of which is:
for(var i=0; i < MAX_ROWS + 1; i++){
for(var o=0; o < MAX_COLS + 1; o++){
var currentTile:MemberBox = new MemberBox();
currentTile.x = i*150;
currentTile.y = o*25;
currentTile.name = "b"+o;
memberBox.addChild(currentTile);
}}
Now I need to add a textfield to each box, which will later be populated with data from an array. I tried adding each textfield to an array in the for loop and then calling from the array, but the textfields still all have the same name so only the last one called actually works...
Here is what I have - it almost does what I need, but it only adds text to the last box created.
var txtArray:Array = new Array();
for(var i=0; i < MAX_ROWS + 1; i++){
for(var o=0; o < MAX_COLS + 1; o++){
var currentTile:MemberBox = new MemberBox();
currentTile.x = i*150;
currentTile.y = o*25;
currentTile.name = "b"+o;
memberBox.addChild(currentTile);
currentTile.addChild(memberBoxText);
memberBoxText.width = 150;
memberBoxText.height = 25;
txtArray[o] = memberBoxText;
txtArray[o].text = "test"+o;
}}
Well you didn't declare anywhere memberBoxText so I asume you added it manually through flash builder. You are not making new instance of your textField.Try inserting this into for loop:
var memberTxt:TextField=new TextField(); currentTile.addChild(memberTxt);
:)

Ignore blank values in WPF chart control

I am working on WPF application which includes WPF chart. I am facing a situation.
I want to draw the chart only for values by ignoring blank values.
In application the data is contained by a datagrid and same data will be reflected in the graph, but datagrid having blank values(DBNull.Value).
So, I want to generate graph with only values by ignoring the blank values.
Here is my code for generating graph.
for (int col = 1; col < dtGeneric.Columns.Count; col++)
{
valueList = new List<KeyValuePair<string, double>>();
gLineSeries= new System.Windows.Controls.DataVisualization.Charting.LineSeries();
for (int row = 0; row < dtGeneric.Rows.Count - 1; row++)
{
if (string.IsNullOrEmpty(XaxisValue))
{
XaxisValue = "0";
}
YAxisValue = dtGeneric.Rows[row][col].ToString();
if (string.IsNullOrEmpty(YAxisValue))
{
YAxisValue = "0";
}
valueList.Add(new KeyValuePair<string, double>(XaxisValue, Convert.ToDouble(YAxisValue)));
}
gLineSeries.DependentValuePath = "Value";
gLineSeries.Style = gLineSeries.PolylineStyle;
gLineSeries.IndependentValuePath = "Key";
gLineSeries.ItemsSource = valueList;
gLineSeries.Title = dtGeneric.Columns[col].Caption.Replace('_', '.').ToString();
gLineSeries.AnimationSequence = AnimationSequence.FirstToLast;
chartControl.Series.Add(gLineSeries);
}
}
As you can see in the code, I have used keyvaluepair to draw the graph. So I am unable to add null value in the Value of keyvaluepair. I have tried with double.NaN but that is not working.
I have iterated all the columns because all the column will have its separate graph.
I have tried one logic to create the graph which is:
for (int col = 1; col < dtGeneric.Columns.Count; col++)
{
valueList = new List<KeyValuePair<string, double>>();
gPositionLineSeries = new System.Windows.Controls.DataVisualization.Charting.LineSeries();
for (int row = 0; row < dtGeneric.Rows.Count - 1; row++)
{
if (!string.IsNullOrEmpty(dtGeneric.Rows[row][0].ToString()) && !string.IsNullOrEmpty(dtGeneric.Rows[row][col].ToString())) //Null values will be ignored for graph generation...
{
XaxisValue = dtGeneric.Rows[row][0].ToString();
YAxisValue = dtGeneric.Rows[row][col].ToString();
valueList.Add(new KeyValuePair<string, double>(XaxisValue, Convert.ToDouble(YAxisValue)));
}
else
{
continue;
}
}
}
Above code is working fine but the X-axis values in the graph is not in order.
Please tell me some solution.
use below code in if block
string.IsNullOrEmpty(row.Cells[clm.Index].Value.ToString())

How to bind values Programatically created Textbox in a for loop

The Stack panels and Textbox are programmatically in a for-loop, by just changing the margins. Then these are inturn added to the Window context.
Now, When the user enters the values in the TextBox, I need to bind them to seperate fields. But, for some reason, the Textbox is not enabled and I think the reason is the fields are not binded properly.
StackPanel panel1 = new StackPanel();
for (int i = 0; i < 10; i++)
{
TextBox txtProgramValue1 = new TextBox();
txtProgramValue1.FontSize = 14;
txtProgramValue1.Height = 32;
txtProgramValue1.HorizontalAlignment = HorizontalAlignment.Right;
txtProgramValue1.VerticalAlignment = VerticalAlignment.Top;
txtProgramValue1.Width = 126;
txtProgramValue1.Margin = new Thickness(0, 51 + (i * 100), 16, 0);
txtProgramValue1.Name = "lblProgramValue" + i.ToString();
txtProgramValue1.IsEnabled = true;
panel1.Children.Add(txtProgramValue1);
}
I need to map the txtProgramValue1.Text of each TextBox to a list.
The use of Textbox as an array
Textbox[] _Textbox = new Textbox[5];
for(int i=0; i < 5;i++)
{
_Textbox[i] = new Textbox();
}
This solves this question.

Resources