Getting new line in wpf - wpf

i am having arraylist.let say it contains 15 elements. I am adding these to stack panel. I need to add 3 elements per line. my code is below. I am getting either horizontal or verrtical.Let me know how to do this.
MainWindow w;
public ShopCart(MainWindow m,ArrayList _list)
{
InitializeComponent();
w = m;
int i = 1;
foreach (string cartitems in _list)
{
mystackpanel.Orientation = Orientation.Horizontal;
mystackpanel.Margin.Left.Equals(150);
Label lbl = new Label();
lbl.Name = "Label" + i;
lbl.Height = 30;
lbl.Width = 200;
lbl.Margin.Left.Equals(150);
//lbl.Margin.Top.Equals(150);
lbl.Content = cartitems.ToString();
mystackpanel.Children.Add(lbl);
i++;
int str = mystackpanel.Children.Count;
MessageBox.Show(Convert.ToString(str));
if (str%3 == 0)
{
Button btndelete = new Button();
btndelete.Content = "Delete";
btndelete.Width = 120;
btndelete.Height = 35;
mystackpanel.Children.Add(btndelete);
mystackpanel.Margin.Top.Equals(500);
}
}

Here is some sample code(assuming you already have a list of buttons, and will add outer stack panel to your main control) you can try, you may need to change few things as per your need:
List<Button> buttons = new List<Button>();
StackPanel panel = new StackPanel();
panel.Orientation = Orientation.Horizontal;
int count = 0;
StackPanel innerPanel = new StackPanel();
innerPanel.Orientation = Orientation.Vertical;
foreach (Button button in buttons)
{
innerPanel.Children.Add(button);
++count;
if (count % 3 == 0 && count != 0)
{
panel.Children.Add(innerPanel);
innerPanel = new StackPanel();
innerPanel.Orientation = Orientation.Vertical;
}
}
if (panel.Children.Contains(innerPanel) == false)
{
panel.Children.Add(innerPanel);
}
Although in my opinion the best way will be to have a Grid with n*n row and columns and add your buttons to respective row, columns.

Related

Better way to get values of datagrid cell

i have got dategrid which populated from sql query
HPEntities db = new HPEntities();
var queryTable4 = db.Database.SqlQuery<Lbrctn>("select * from Lbrctn");
var u = queryTable4.ToList();
DG_Example.ItemsSource = u;
i want to get the values of those row of my dataGrid which has checked. and send to Stored Procedure
so i try this code:
for (int i = 0; i < DG_Example.Items.Count - 1; i++)
{
mChkBox = DG_Example.Columns[0].GetCellContent(DG_Example.Items[i]) as CheckBox;
if (mChkBox.IsChecked == true)
{
var DaSelect = DG_Example.Columns[1].GetCellContent(DG_Example.Items[i]) as TextBlock;
var MNameTBk = DG_Example.Columns[2].GetCellContent(DG_Example.Items[i]) as TextBlock;
var ChiCodeTBk = DG_Example.Columns[3].GetCellContent(DG_Example.Items[i]) as TextBlock;
var ChiNameTBk = DG_Example.Columns[4].GetCellContent(DG_Example.Items[i]) as TextBlock;
var IntenCodeTBk = DG_Example.Columns[6].GetCellContent(DG_Example.Items[i]) as TextBlock;
var PeDescTBk = DG_Example.Columns[7].GetCellContent(DG_Example.Items[i]) as TextBlock;
db.sp_Ins_inten // using Stored Procedure
(
IntenCodeTBk.Text.Trim(),
MNameTBk.Text.Trim(),
Convert.ToInt32(chiCodeTBk.Text.Trim()),
ChiNameTBk.Text.Trim(),
PeDescTBk.Text.Trim(),
DaSelect.Text.Trim()
);
db.SaveChanges();
}
}
is there a better way than GetCellContent to get values of datagrid row I could use?
thanx in advance
all values can be found in a data item, displayed in a DataGridRow (except probably CheckBox, which is likely not bound):
for (int i = 0; i < DG_Example.Items.Count - 1; i++)
{
mChkBox = DG_Example.Columns[0].GetCellContent(DG_Example.Items[i]) as CheckBox;
if (mChkBox.IsChecked == true)
{
Lbrctn item = DG_Example.Items[i] as Lbrctn;
db.sp_Ins_inten // using Stored Procedure
(
item.IntenCodeTBk,
item.MNameTBk,
item.Convert,
item.ChiNameTBk,
item.PeDescTBk,
item.DaSelect
);
db.SaveChanges();
}
}

How to paint a specific cell of a stackpanel?

In order to draw the matrix of cells , I used a Stackpanel, defined by this code:
int columns = Convert.ToInt32(columnasText.Text);
int rows = Convert.ToInt32(filasText.Text);
SolidColorBrush selected1 = new SolidColorBrush(Colors.Aquamarine);
SolidColorBrush released = new SolidColorBrush(Colors.White);
for (int i = 0; i < rows; i++)
{
StackPanel stkPanel = new StackPanel();
stkPanel.Orientation = Orientation.Horizontal;
for (int j = 0; j < columns; j++)
{
Label lbl = new Label();
lbl.Height = rejilla.Height / rows;
lbl.Width = rejilla.Width / columns;
lbl.Tag = new Point(i, j);
lbl.BorderBrush = new SolidColorBrush(Colors.Black);
lbl.BorderThickness = new Thickness(1);
lbl.Background = released;
stkPanel.Children.Add(lbl);
}
rejilla.Children.Add(stkPanel);
Once is defined, I need to change the colours of each cell depending on the values of each, and I'm not able to do it.
I used your variable rejilla (I am assuming it is either StackPanel or Grid). Either way, it will work.
Method 1:
The key is using .Children.OfType()
//Get your cell location
int rowIndex = 2;
int columnIndex = 8;
//Get your desired new color
SolidColorBrush selected1 = new SolidColorBrush(Colors.Aquamarine);
//Get list of your row panels
var stackPanels = rejilla.Children.OfType<StackPanel>().ToList();
//Check if desired row panel exist
if (rowIndex < stackPanels.Count && rowIndex >= 0)
{
//Get list of your labels in the desired row panel
var labels = stackPanels[rowIndex].Children.OfType<Label>().ToList();
//Check if desired cell exist or not then change background
if (columnIndex < labels.Count && columnIndex >= 0)
labels[columnIndex].Background = selected1;
}
Method 2: Using your Tag that you set (Point)
Not recommended, this method would have been useful if you placed all your labels in one stack panel instead of placing them in multiple stack panels (one stack panel for each row).
//Get your cell location
int rowIndex = 2;
int columnIndex = 8;
//Get your desired new color
SolidColorBrush selected1 = new SolidColorBrush(Colors.Aquamarine);
//Get list of your row panels
var stackPanels = rejilla.Children.OfType<StackPanel>().ToList();
//Check if desired row panel exist
if (rowIndex < stackPanels.Count && rowIndex >= 0)
{
//Get list of your labels in the desired row panel
var label = stackPanels[rowIndex].Children.OfType<Label>()
.Where(Item => (int)(Item.Tag as Nullable<Point>).GetValueOrDefault().X == rowIndex
&& (int)(Item.Tag as Nullable<Point>).GetValueOrDefault().Y == columnIndex).FirstOrDefault();
if(label != null)
label.Background = selected1;
}
You can place the code inside a method and pass your stackPanel, color, rowIndex, columnIndex:
private void SetCellColor(StackPanel stackPanel, SolidColorBrush color, int rowIndex, int columnIndex)
{
//Get list of your row panels
var stackPanels = stackPanel.Children.OfType<StackPanel>().ToList();
//Check if desired row panel exist
if (rowIndex < stackPanels.Count && rowIndex >= 0)
{
//Get list of your labels in the desired row panel
var labels = stackPanels[rowIndex].Children.OfType<Label>().ToList();
//Check if desired cell exist or not then change background
if (columnIndex < labels.Count && columnIndex >= 0)
labels[columnIndex].Background = color;
}
}
Then call it:
SetCellColor(rejilla, new SolidColorBrush(Colors.Aquamarine), rowIndex, columnIndex);
Good Luck!

Why does winforms CheckBox not meet top of container

I am trying to "pleasantly" align tops of control in a left-to-right flowpanel.
I have a checkbox and several numericUpDowns. BUT the checkbox always has some sort of margin or padding at the top. So, I have to add margn at top of numericUpDown to get them to line up.
This isn't ideal. Why can't I get checkBox to start directly below top of flowpanel, just like numericUpDown does?
public partial class Form1 : Form
{
public static FlowLayoutPanel ControlPanel;
public void CreateControls()
{
ControlPanel = new FlowLayoutPanel();
ControlPanel.SuspendLayout();
ControlPanel.FlowDirection = FlowDirection.LeftToRight;
ControlPanel.Height = 40;
ControlPanel.Width = this.flowLayoutPanel1.Width - 10;
ControlPanel.WrapContents = false;
ControlPanel.AutoScroll = true;
ControlPanel.Anchor = AnchorStyles.None;
ControlPanel.BorderStyle = BorderStyle.FixedSingle;
ControlPanel.Controls.Add(LabelWithText("LB", 60));
AddNumericUpDown("Ledge", 20, 0, 1m, -500m, 500m);
AddCheckBox("UseExit", "UseExit", false);
AddNumericUpDown("WrongLedge", 30, 0, 1m, -500m, 500m);
AddNumericUpDown("Max", 100000, 0, 100000m, 0m, 1000000m);
ControlPanel.ResumeLayout();
this.flowLayoutPanel1.Controls.Add(ControlPanel);
}
public Label LabelWithText(string text, int width)
{
Label label = new Label();
label.Text = text;
label.Width = width;
label.Top = 0;
return label;
}
public void AddCheckBox(string name, string text, bool initialValue)
{
CheckBox checkBox = new CheckBox();
checkBox.Name = name;
checkBox.Text = text;
checkBox.Checked = initialValue;
checkBox.Top = 0;
checkBox.Margin = new Padding(5,0,0,0);
checkBox.TextAlign = ContentAlignment.TopLeft;
checkBox.CheckAlign = ContentAlignment.TopLeft;
checkBox.ImageAlign = ContentAlignment.TopLeft;
ControlPanel.Controls.Add(checkBox);
}
public void AddNumericUpDown(string name, double initialValue, int decimalPlaces, decimal increment, decimal minimum, decimal maximum)
{
NumericUpDown numericUpDown = new NumericUpDown();
numericUpDown.DecimalPlaces = decimalPlaces;
numericUpDown.Increment = increment;
numericUpDown.Minimum = minimum;
numericUpDown.Maximum = maximum;
numericUpDown.Value = (decimal)initialValue;
numericUpDown.Name = name;
numericUpDown.Visible = true;
numericUpDown.Margin = new Padding(2);
numericUpDown.Top = 0;
numericUpDown.Width = 80;
ControlPanel.Controls.Add(LabelWithText(name, 70));
ControlPanel.Controls.Add(numericUpDown);
}
}
It's not the tops that are supposed to align. The baseline of the text is what's supposed to align between various controls. This is more evident if you use a larger font.

Problem with image column in XtraTreeList

I use XtraTreeList control.
There are 2 columns: first for text and second for icon
Problem : I can't change default icon (zero index in corresponding imagelist). There are 3 images in imagelist.
For example I need to show icon which is located at 2 index
Code
TreeListColumn col = treeList1.Columns.Add();
col.Caption = "Text";
col.Visible = true;
TreeListColumn colImage = treeList1.Columns.Add();
colImage.Caption = "ImageColumn";
colImage.Visible = true;
RepositoryItemImageEdit imageEdit = new RepositoryItemImageEdit();
imageEdit.Images = imageList;
treeList1.RepositoryItems.Add(imageEdit);
colImage.ColumnEdit = imageEdit;
treeList1.BeginUnboundLoad();
TreeListNode node = treeList1.AppendNode(new object[] { "trololo", 2}, null);
node.SetValue(colImage.AbsoluteIndex, 2);
treeList1.EndUnboundLoad();
Thanks for everybody
Using RepositoryItemPictureEdit solved my problem. A little bit complex, but works
TreeListColumn col = treeList1.Columns.Add();
col.Caption = "Text";
col.Visible = true;
TreeListColumn colImage = treeList1.Columns.Add();
colImage.Caption = "ImageColumn";
colImage.Visible = true;
RepositoryItemPictureEdit imageEdit = new RepositoryItemPictureEdit();
imageEdit.ShowMenu = false;
treeList1.RepositoryItems.Add(imageEdit);
colImage.ColumnEdit = imageEdit;
treeList1.BeginUnboundLoad();
Image img = imageList.Images[1];
Bitmap bmp = new Bitmap(img);
TreeListNode node = treeList1.AppendNode(new object[] { "trololo", bmp }, null);
treeList1.EndUnboundLoad();
This task should be implemented using slightly different approach. First, you should use the RepositoryItemImageComboBox and populate its Items property. Each item has value and ImageIndex. The TreeList will show in a cell image from the item whose value equals the cell value. Here is the code which should work for you:
TreeListColumn col = treeList1.Columns.Add();
col.Caption = "Text";
col.Visible = true;
TreeListColumn colImage = treeList1.Columns.Add();
colImage.Caption = "ImageColumn";
colImage.Visible = true;
RepositoryItemImageComboBox imageEdit = new RepositoryItemImageComboBox();
imageEdit.SmallImages = imageList;
for(int i = 0; i < 3; i++)
imageEdit.Items.Add(new ImageComboBoxItem(i, i)); // i.e. value and image index
treeList1.RepositoryItems.Add(imageEdit);
colImage.ColumnEdit = imageEdit;
treeList1.BeginUnboundLoad();
TreeListNode node = treeList1.AppendNode(new object[] { "trololo", 2 }, null);
node.SetValue(colImage.AbsoluteIndex, 2);
treeList1.EndUnboundLoad();

Silverlight - Fill a rectangle with animation on mouseclick

I want to be able to fill a rectangle with an animation on leftmousebuttondown (this will later be changed to run on load).
My rectangles are drawn to a canvas in code behind based on the data that is passed (one rectangle per row of data)
At the moment they are filled with a static image but I want this Fill to be an animation, a spinner if I can.
I am very new to Silverlight and am not sure how to achieve this. Can someone point me in the right direction?
My code (part) so far.
XAML:
<Canvas x:Name="Grid" Background="LightGray"></Canvas>
CS:
public partial class ProductView : UserControl
{
Processing processingDialog = new Processing();
private int colsRequired = 0;
private int rowsRequired = 0;
private const int minSize = 5;
private int cellSize = 1;
public ProductView()
{
InitializeComponent();
}
public void UpdateGrid(ObservableCollection<Product> productList)
{
calculateRowsCols(productList);
drawGrid(productList);
}
public void calculateRowsCols(ObservableCollection<Product> productList)
{
int tileCount = productList.Count();
double tileHeight = Grid.ActualHeight;
double tileWidth = Grid.ActualWidth;
if (tileCount == 0)
return;
double maxSize = Math.Sqrt((tileHeight * tileWidth) / tileCount);
double noOfTilesHeight = Math.Floor(tileHeight / maxSize);
double noOfTilesWidth = Math.Floor(tileWidth / maxSize);
double total = noOfTilesHeight * noOfTilesWidth;
cellSize = (maxSize < minSize) ? minSize : Convert.ToInt32(maxSize);
while ((cellSize >= minSize) && (total < tileCount))
{
cellSize--;
noOfTilesHeight = Math.Floor(tileHeight / cellSize);
noOfTilesWidth = Math.Floor(tileWidth / cellSize);
total = noOfTilesHeight * noOfTilesWidth;
}
rowsRequired = Convert.ToInt32(Math.Floor(tileHeight / cellSize));
colsRequired = Convert.ToInt32(Math.Floor(tileWidth / cellSize));
}
private void drawCell(int row, int col, string label, Color fill)
{
Rectangle innertec = new Rectangle();
innertec.Height = cellSize * 0.7;
innertec.Width = cellSize * 0.9;
innertec.StrokeThickness = 1;
innertec.Stroke = new SolidColorBrush(Colors.Black);
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage(new Uri("Assets/loading.png", UriKind.Relative));
innertec.Fill = imageBrush;
Grid.Children.Add(innertec);
Canvas.SetLeft(innertec, (col * cellSize) + ((cellSize - innertec.Width) / 2));
Canvas.SetTop(innertec, row * cellSize + 4);
Border productLabelBorder = new Border();
Grid.Children.Add(productLabelBorder);
Canvas.SetLeft(productLabelBorder, col * cellSize);
Canvas.SetTop(productLabelBorder, row * cellSize);
TextBlock productLabel = new TextBlock();
productLabel.Margin = new Thickness(0, innertec.Height + 5, 0, 5);
productLabel.TextAlignment = TextAlignment.Center;
productLabel.TextWrapping = TextWrapping.NoWrap;
productLabel.TextTrimming = TextTrimming.WordEllipsis;
productLabel.MaxWidth = cellSize;
productLabel.Height = cellSize * 0.3;
productLabel.Width = cellSize;
productLabel.Text = label;
productLabel.HorizontalAlignment = HorizontalAlignment.Center;
productLabel.VerticalAlignment = VerticalAlignment.Center;
productLabel.FontSize = cellSize * 0.13;
ToolTipService.SetToolTip(productLabel, label);
productLabelBorder.Child = productLabel;
}
public void drawGrid(ObservableCollection<Product> data)
{
int dataIndex = 0;
Grid.Children.Clear();
for (int i = 0; i < rowsRequired; i++)
{
for (int j = 0; j < colsRequired; j++)
{
Product product = (dataIndex < data.Count) ? data.ElementAt(dataIndex) : null;
if (product != null)
{
drawCell(i, j, product.productName, Colors.White);
}
dataIndex++;
}
}
}
}
Any help anyone can give, even a pointer in the right direction would be great.
Thanks in advance
Try creating custom control which will encapsulate everything you want from rectangle to do.
you can add new VisualState "MouseDownState" and do required animatin in xaml.
Please let me know if you need more details regarding the implementation.
late simply add new control instead of rectangle.

Resources