this is my very first program in WP7, so i have some issues.
I'm trying to populate a grid with an list of objects that has been bought by another method. Here what i made so far:
public partial class MainPage : PhoneApplicationPage
{
private List<Row> lsResult;
private Grid myGrid = new Grid();
private int i = 0;
// Constructor
public MainPage()
{
InitializeComponent();
ColumnDefinition colData = new ColumnDefinition();
ColumnDefinition colOcorrencia = new ColumnDefinition();
ColumnDefinition colSituacao = new ColumnDefinition();
myGrid.ColumnDefinitions.Add(colData);
myGrid.ColumnDefinitions.Add(colOcorrencia);
myGrid.ColumnDefinitions.Add(colSituacao);
myGrid.ShowGridLines = true;
SolidColorBrush myBrush = new SolidColorBrush(Colors.White);
myGrid.Background = myBrush;
gridResult = myGrid;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
i = 0;
lsResult = null;
lsResult = Rastrear.Busca(txtNumber.Text);
foreach (Row r in lsResult)
{
RowDefinition rNewRow = new RowDefinition();
myGrid.RowDefinitions.Add(rNewRow);
TextBlock lblData = new TextBlock();
lblData.Text = r.Data.ToString();
lblData.HorizontalAlignment = HorizontalAlignment.Center;
lblData.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumnSpan(lblData, 1);
Grid.SetRow(lblData, i);
TextBlock lblOcorrencia = new TextBlock();
lblOcorrencia.Text = r.Ocorrencia.ToString() ;
Grid.SetColumnSpan(lblOcorrencia, 2);
Grid.SetRow(lblOcorrencia, i);
TextBlock lblSituacao = new TextBlock();
lblSituacao.Text = r.Situacao.ToString();
Grid.SetColumnSpan(lblSituacao, 3);
Grid.SetRow(lblSituacao, i);
i++;
myGrid.Children.Add(lblData);
myGrid.Children.Add(lblOcorrencia);
myGrid.Children.Add(lblSituacao);
}
gridResult = myGrid;
}
}
The method Buscar() is returning the list as it should, but when i click on the button it doesn't do anything, not even the paint to white on the constructor happens actually.
thanks in advance
You're doing some pretty shady stuff in your code behind that i'm used to seeing and should be done in xaml. Change this
gridResult = myGrid;
to
gridResult.Children.Add(myGrid);
I don't think you can just change references of static UI elements like that.
Related
I've got the following event:
private void PlaceToken(object sender, RoutedEventArgs e)
{
ConnectFourViewModel currentViewModel = (ConnectFourViewModel)DataContext;
Button button = (Button)sender;
int currentColumn = Convert.ToInt32(button.Content);
int currentPlayer = currentViewModel.Board.CurrentPlayer;
currentViewModel.Board.PlaceToken(currentColumn, currentPlayer);
}
After the following set up:
public MainWindow()
{
var window = new Window();
var grid = new Grid {};
ConnectFourViewModel ViewModel = new ConnectFourViewModel();
//Set up rows and cols
for(int i = 1; i<=7; i++)
{
var col = new ColumnDefinition();
grid.ColumnDefinitions.Add(col);
}
for (int i = 1; i <= 7; i++)
{
var row = new RowDefinition();
grid.RowDefinitions.Add(row);
}
//Set up tiles
foreach (var item in ViewModel.Board.AllTiles)
{
int index = ViewModel.Board.AllTiles.IndexOf(item);
string name =
"Col" +
Convert.ToString(item.Column) +
"_Row" +
Convert.ToString(item.Row);
Label currentTile = new Label{ Name = name};
Grid.SetRow(currentTile, item.Row - 1);
Grid.SetColumn(currentTile, item.Column -1);
//Bind
var binding = new Binding();
binding.Source = ViewModel.Board.AllTiles[index];
binding.Path = new PropertyPath("Contents");
currentTile.SetBinding(ContentProperty, binding);
//Add
grid.Children.Add(currentTile);
}
//Set up Buttons
for (int i = 1; i <= 7; i++)
{
Button currentButton = new Button { };
//binding
var binding = new Binding();
binding.Source = ViewModel.CurrentColumn;
currentButton.SetBinding(ContentProperty, binding);
//Set Column names, this has to be after the binding has been set.
currentButton.Content = i;
//events
currentButton.Click += new RoutedEventHandler(PlaceToken);
//add
Grid.SetColumn(currentButton, i - 1);
Grid.SetRow(currentButton, 7);
grid.Children.Add(currentButton);
}
window.Content = grid;
window.DataContext = ViewModel;
window.Show();
InitializeComponent();
}
I'm expecting the line ConnectFourViewModel currentViewModel = (ConnectFourViewModel)DataContext; to set currentViewModel to reflect the information my UI is running off. Unfortunately it's returning null and I'm not sure why.
This is clearly highlighting a gap in my understanding of the topic, but unsure on how to tackle it I could use a hand.
Any idea where I've gone wrong?
You haven't assigned the DataContext to ViewModel anywhere. Assuming you want the ViewModel to be the ViewModel of MainWindow, I suggest you assigne it before Initialize Component as shown below. Since it's never assigned you are getting null.
public MainWindow()
{
ConnectFourViewModel ViewModel = new ConnectFourViewModel();
. . .
. . .
DataContext = ViewModel ;
InitializeComponent();
}
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.
I am working in silverlight and under a very strange situation that i need to implement my own SelectionChangedEventHandler handler function .The reason to do so is:
I have my situation like this :
private static Grid GenerateList(Parameter param, int LoopCount, Grid g)
{
Grid childGrid = new Grid();
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
childGrid.ColumnDefinitions.Add(colDef1);
childGrid.ColumnDefinitions.Add(colDef2);
childGrid.ColumnDefinitions.Add(colDef3);
TextBlock txtblk1ShowStatus = new TextBlock();
TextBlock txtblkLabel = new TextBlock();
ListBox lines = new ListBox();
ScrollViewer scrollViewer = new ScrollViewer();
scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
lines.ItemsSource = param.Component.Attributes.Items;
scrollViewer.Content = lines;
Grid.SetColumn(scrollViewer, 1);
Grid.SetRow(scrollViewer, LoopCount);
childGrid.Children.Add(scrollViewer);
lines.SelectedIndex = 0;
lines.SelectedItem = param.Component.Attributes.Items;// This items contains 1000000,3 00000, and so on.
lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged);
// lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged(lines, txtblk1ShowStatus));
lines.SelectedIndex = lines.Items.Count - 1;
Grid.SetColumn(txtblk1ShowStatus, 2);
Grid.SetRow(txtblk1ShowStatus, LoopCount);
childGrid.Children.Add(txtblk1ShowStatus);
g.Children.Add(childGrid);
return (g);
}
static void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show("clist _SelectionChanged1");
//The problem is line below because i am "lines" and "txtblk1ShowStatus" are not in the
//scope of this function and i cannot declare them globally.
txtblk1ShowStatus.Text = lines[(sender as ListBox).SelectedIndex];
}
Here you can see that i have no access of of "lines" and "txtblk1ShowStatus" in the List_SelectionChanged(object sender, SelectionChangedEventArgs e) function. And i cannot declate the button and list globally because this function GenerateList(...) will be reused and it's just coded in c# (no xaml used).Please let me know how to do that and also explain how to do that if you have another way to do but please explain your code with detail
I think a lambda expression will help you solve your issues (you might need to edit it):
lines.SelectedIndex = 0;
lines.SelectedItem = param.Component.Attributes.Items;// This items contains 1000000,3 00000, and so on.
lines.SelectionChanged += (o,e) => {
MessageBox.Show("clist _SelectionChanged1");
txtblk1ShowStatus.Text = lines.SelectedItem.ToString();
};
lines.SelectedIndex = lines.Items.Count - 1;
I am using c# in silverlight application.
I have a grid with 1 row and 3 columns.
There are 2 things that i am not able to know how to do:
(1) I have to display the boundaries of just row (not columns, only rows). How to do that?
Currently i have a grids like this:
//The p in function call below is yhe object obtained on deserialixing xml.
private static Grid GenerateGrid(Parameters p)
{
Grid myGrid = new Grid();
myGrid.Width = 650;
myGrid.HorizontalAlignment = HorizontalAlignment.Left;
myGrid.VerticalAlignment = VerticalAlignment.Top;
myGrid.ShowGridLines = false;
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
myGrid.ColumnDefinitions.Add(colDef1);
myGrid.ColumnDefinitions.Add(colDef2);
myGrid.ColumnDefinitions.Add(colDef3);
int totalRows = p.Parameter.Count() + p.Separator.Count();
for (int i = 0; i < totalRows; i++)
{
myGrid.RowDefinitions.Add(new RowDefinition());
}
return (myGrid);
}
Call to this function is :
XmlParameterClasses.Parameters parameter =
(XmlParameterClasses.Parameters)deserializer.Deserialize(reader);
Grid BigGrid = GenerateGrid(parameter);
My try to achieve is this: (I used Border to do this, see at the end of the function)
private static Grid GenerateComboBox(ViewModel.XmlParameterClasses.Parameter param, int LoopCount, Grid g)
{ //param is the object of the class Parameter
StackPanel sp1 = new StackPanel(); //These three stackpanels are inside the grid cell
StackPanel sp2 = new StackPanel();
StackPanel sp3 = new StackPanel();
ComboBox cb = new ComboBox();
TextBlock txtblk1 = new TextBlock();
TextBlock txtblkLabel = new TextBlock();
////////////////////////////////////
//Label Display
txtblkLabel.Text = param.Label;
txtblkLabel.VerticalAlignment = System.Windows.VerticalAlignment.Center;
txtblkLabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
txtblkLabel.TextAlignment = System.Windows.TextAlignment.Center;
txtblkLabel.FontWeight = FontWeights.Bold;
txtblkLabel.FontSize = 15;
txtblkLabel.FontStyle = FontStyles.Normal;
txtblkLabel.Padding = new Thickness(5, 10, 5, 10);
sp1.Orientation = Orientation.Horizontal;
sp1.Children.Add(txtblkLabel);
sp1.Width = 100;
sp1.Height = 50;
Grid.SetRow(sp1, LoopCount);
Grid.SetColumn(sp1, 0);
g.Children.Add(sp1);
foreach(var item in param.Component.Attributes.Items) {
cb.Items.Add(item);
}
cb.SelectionChanged += new SelectionChangedEventHandler(comboBox1_SelectionChanged);
cb.SelectedIndex = cb.Items.Count - 1;
//For text Display
txtblk1.Text = cb.SelectedValue.ToString() + " millions";
txtblk1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
txtblk1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
txtblk1.TextAlignment = System.Windows.TextAlignment.Center;
txtblk1.FontWeight = FontWeights.Bold;
txtblk1.FontSize = 15;
txtblk1.FontStyle = FontStyles.Normal;
txtblk1.Padding = new Thickness(5, 10, 5, 10);
sp2.Orientation = Orientation.Horizontal;
sp2.Children.Add(txtblk1);
Grid.SetColumn(sp2, 2);
Grid.SetRow(sp2, LoopCount);
g.Children.Add(sp2);
//For combo box display
cb.Width = 45;
cb.Height = 25;
sp3.Orientation = Orientation.Horizontal;
sp3.Children.Add(cb);
sp3.Width = 50;
sp3.Height = 50;
Grid.SetColumn(sp3, 1);
Grid.SetRow(sp3, LoopCount);
g.Children.Add(sp3);
////////////Here is the Border Display ////////////////////////////
Border rect = new Border();
rect.Width = g.Width;
rect.Height = g.Height;
rect.BorderThickness = new Thickness(5);
rect.BorderBrush = new SolidColorBrush(Colors.Black);
g.Children.Add(rect);
////////////////////////////////////////////////////////////////////
return (g);
}
but the output obtained is like this : (It just cover the border of first cell not the other two in that row, whereas i just want one border over one row(not over the column in that row, just row boundary))
Could some one please help me in achieving this step ? Is it possible to implement what i want trying to do?
Note: Please note that code has to be implemented using c# only , not xaml.
I have done it by creating a grid of 1 column and rows (instead of 3*3 cell it must be 1*3 (row*column)). Then creating Border in each row and then again creating grid with 1 row and 3 columns and then creating border of this small grid.
code is:
Border rect = new Border();
rect.Width = g.Width;
rect.Height = g.Height;
rect.BorderThickness = new Thickness(2);
rect.BorderBrush = new SolidColorBrush(Colors.Black);
Grid childGrid = new Grid();
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
childGrid.ColumnDefinitions.Add(colDef1);
childGrid.ColumnDefinitions.Add(colDef2);
childGrid.ColumnDefinitions.Add(colDef3);
TextBlock txtblk3 = new TextBlock();
var border = new Border()
{
Background = new SolidColorBrush(Colors.LightGray)
};
border.Height = 14;
var border1 = new Border()
{
Background = new SolidColorBrush(Colors.White)
};
border1.Height = 14;
Grid.SetColumnSpan(border, 3);
Grid.SetRow(childGrid, LoopCount);
childGrid.Children.Add(border);
txtblk3.FontSize = 14;
txtblk3.FontWeight = FontWeights.Bold;
txtblk3.Text = param.Separator[SeparatorPosition];
Grid.SetColumn(border1, 1);
Grid.SetRow(border1,LoopCount);
border1.Child = txtblk3;
childGrid.Children.Add(border1);
g.Children.Add(childGrid);
return (g);
Where "g" has only 1 column and "LoopCount" numbers of row. And it worked for me.
You should override CellPainting event of DataGridView control as the code snippet below:
private void grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
e.AdvancedBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None;
e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;
}
I want to change content of button1 on click event of button2 . But not able to get the object of grid's child Button class which is in List<> UiList.
Please Guide me in getting the right approach to look it and solve it . And also guide that if the object is build in runtime then how to access it ?
public partial class MainPage : PhoneApplicationPage
{
List<Grid> UIList = new List<Grid>();
Grid objGrid1 = null;
Button objButton1 = null;
Button objButton2 = null;
// Constructor
public MainPage()
{
InitializeComponent();
createGrid1("grid1");
createButton2("Button2");
}
public void createGrid1(string x)
{
objGrid1 = new Grid();
objGrid1.Height = 100;
objGrid1.Name = x;
objGrid1.Width = 200;
objGrid1.Margin = new Thickness(100, 100, 0, 0);
objGrid1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
objGrid1.VerticalAlignment = System.Windows.VerticalAlignment.Top;
objGrid1.Background = new SolidColorBrush(Colors.Orange);
createButton1("changename");
}
public void createButton1(string _name)
{
objButton1 = new Button();
objButton1.Height = 90;
objButton1.Name = _name;
objButton1.Content="Button1";
objButton1.FontSize = 20;
objButton1.Width = 190;
objButton1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
objButton1.VerticalAlignment = System.Windows.VerticalAlignment.Top;
objButton1.Background = new SolidColorBrush(Colors.Blue);
objButton1.Foreground = new SolidColorBrush(Colors.White);
objGrid1.Children.Add(objButton1);
LayoutRoot.Children.Add(objGrid1);
UIList.Add(objGrid1);
}
public void createButton2(string _name)
{
objButton2 = new Button();
objButton2.Margin = new Thickness(240, 300, 0, 0);
objButton2.Name = _name;
objButton2.Height = 90;
objButton2.Content = "Button2";
objButton2.FontSize = 20;
objButton2.Width = 190;
objButton2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
objButton2.VerticalAlignment = System.Windows.VerticalAlignment.Top;
objButton2.Background = new SolidColorBrush(Colors.Blue);
objButton2.Foreground = new SolidColorBrush(Colors.White);
LayoutRoot.Children.Add(objButton2);
objButton2.Click += (s, e) =>
{
int c = UIList.ElementAt(0).Children.Count;
if (c == 1)
{
//logic to change content of Button1 on click of Button2
}
};
}
}
Assuming you cannot just keep a reference to the created control, such as in a class field, you can iterate through the Children property of the Grid and find the desired Button. If there are multiple buttons, you can differentiate them using the Tag property.
Once you find it, change the contents of the button using the Content property, as discussed in the contents above.