How to implement my own SelectionChangedEventHandler in c# - c#

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;

Related

(ViewModel)DataContext returning null

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();
}

Accessing TextBlock in a grid with LINQ

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.

How to check if a space on the form has an object

I'm working on a mind-map project. I'm trying to get the "New Bubble" button to create a new textbox into a FREE space on the form. So I want to check if there is another bubble in the place where it's getting created. If it already has a textbox then I want it to find a new place and repeat the process.
How can I do this?
public partial class frmMap : Form
{
private void btnProperties_Click(object sender, EventArgs e)
{
new frmProperties().Show();
}
private void btnNewBubble_Click(object sender, EventArgs e)
{
var tb = new TextBox();
tb.Multiline = true;
tb.BorderStyle = BorderStyle.FixedSingle;
tb.Top = 100;
tb.Left = 200;
tb.Size = new Size(100, 100);
this.Controls.Add(tb);
}
}
You can check "collision" with other controls like so:
foreach (Control checkControl in Controls)
{
if (tb.Bounds.IntersectsWith(checkControl.Bounds))
...
}
Of course, thats a lot of checking to do! If you are just going to "grid" the controls, it would be faster/easier to just layout a bool array that holds the state of each "cell" (filled/empty) then pick the first empty one you find.
Create dynamic textbox:
var tb = new TextBox();
tb.Multiline = true;
tb.BorderStyle = BorderStyle.FixedSingle;
tb.Top = 100;
tb.Left = 200;
tb.Size = new Size(100, 100);
Then use Rectangle.IntersectWith to check if new textbox intersects with other already added texboxes (you can remove control type filter, if you have other type of controls to check):
while(Controls.OfType<TextBox>().Any(x => tb.Bounds.IntersectsWith(x.Bounds))
{
// adjust tb size or position here
}
And last step - add textbox to form:
Controls.Add(tb);

How to display only row border of a grid (not column) using Border()

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;
}

How to populate dynamically a grid on WP7?

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.

Categories

Resources