Fill TextBlock within a Button with database records - c#

lets say i have 10 rows in my database table. I want to create 10 Buttons and TextBlock inside Buttons filled with data rows. I already create the 10 Buttons and TextBlock inside it here is the code
for (int i = 0; i < 10; i++)
{
Button Btn = new Button();
Style style = this.FindResource("MetroNewButton") as Style;
Btn.Style = style;
Btn.Width = 250;
Btn.Height = 80;
Btn.Name = "BtnCA" + i;
Btn.FlowDirection = FlowDirection.LeftToRight;
Btn.HorizontalAlignment = HorizontalAlignment.Left;
Btn.VerticalAlignment = VerticalAlignment.Top;
for (int j = 0; j < 10; j++)
{
Grid GridContent = new Grid();
GridContent.Width = 250;
GridContent.Height = 80;
TextBlock txtBlock = new TextBlock();
txtBlock.Name = "txtBlock" + j;
txtBlock.TextWrapping = TextWrapping.Wrap;
txtBlock.FontSize = 14;
txtBlock.FontWeight = FontWeights.Bold;
//txtBlock1.FlowDirection = FlowDirection.RightToLeft;
txtBlock.Padding = new Thickness(10, 20, 0, 0);
txtBlock.VerticalAlignment = VerticalAlignment.Top;
Grid.SetRow(txtBlock, 0);
GridContent.Children.Add(txtBlock);
Btn.Content = GridContent;
}
}
Okay and i have list carries data from database.I want to distribute data to text block, So how to put data in Text block ?

I think, using ItemsControl with proper ItemTemplate would be better idea than creating all the controls in for loop. Just create a list of objects representing your data, bind it to ItemsCOntrol(ItemsSource={Binding your_path}), and set proper item template for grid. After that, put an ItemsControl inside a button. If i understood you right, that should solve your problem.

Instead of using this for loop, you can create buttons using XAML and MVVM binding.
<DataGrid ItemsSource="{Binding btnList}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="{Binding btn_NAME}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Then fetch content from db and fill it into btnList.
To get more idea about MVVM binding, please go through this and this.

Related

Grid does not fill the whole window, why?

I have written a UI for an Game of Life (Automation Theory) and the algorithm works just fine. I have also created a pop-up which will start first and where you can set the amount of rows and columns the game should have. This works also fine. Somehow my Grid only fills a small amount of the space in the window (Like it would only be in the left top cell of another Grid). I cannot work out why this is happening. Help would be much appreciated. I am aware that the code is quit complex and messy at this point. The GameCreation() Method is calling the popup window first. After that I declare some variables and my grid. Then I create rows and columns and button which I store in the grid as well. There are no errors or exceptions.
This is how the window looks like:
private void GameCreation()
{
//Pop-up Fenster aufrufen und eingaben in rowCount und colCount speichern
List<int> list = new List<int>();
list = OptionsWindow();
//Variablen deklarieren
int nameNum = 1;
int rowCount = list[0];
int colCount = list[1];
Button[,] buttons = new Button[rowCount, colCount];
Grid MainGrid = new Grid();
Window1.Content = MainGrid;
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < colCount; j++)
{
// create Grid.RowDefinition and Grid.ColumnDefinition based on rowCount and colCount
RowDefinition row = new RowDefinition();
GridLengthConverter gridLengthConverter = new GridLengthConverter();
row.Height = (GridLength)gridLengthConverter.ConvertFrom("*");
MainGrid.RowDefinitions.Add(row);
ColumnDefinition column = new ColumnDefinition();
column.Width = (GridLength)gridLengthConverter.ConvertFrom("*");
MainGrid.ColumnDefinitions.Add(column);
// Button erstellen
Button button = new Button
{
Name = "B" + nameNum,
Background = Brushes.Gray,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Padding = new Thickness(1, 1, 1, 1)
};
button.Click += ChangeColor;
//den Button zum Grid hinzufügen an der richtigen Stelle
Grid.SetRow(button, i);
Grid.SetColumn(button, j);
MainGrid.Children.Add(button);
//Button buttons[,] hinzufügen
buttons[i, j] = button;
nameNum++;
}
}
//resetButton deklarieren
Button resetButton = new()
{
Name = "resetButton",
Background = Brushes.DodgerBlue,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Padding = new Thickness(1, 1, 1, 1),
Content = new Image
{
Source = new BitmapImage(new Uri("C:\\Users\\myname\\source\\repos\\GameOfLife\\GameOfLifeApp\\ResetButton2.png"))
}
};
resetButton.Click += Reset;
Grid.SetRow(resetButton, rowCount);
Grid.SetColumn(resetButton, 0);
Grid.SetColumnSpan(resetButton, 1);
//StartButton deklarieren
RowDefinition row2 = new RowDefinition();
GridLengthConverter gridLengthConverter2 = new GridLengthConverter();
row2.Height = (GridLength)gridLengthConverter2.ConvertFrom("*");
MainGrid.RowDefinitions.Add(row2);
Button startButton = new()
{
Name = "startButton",
Background = Brushes.DodgerBlue,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Padding = new Thickness(1, 1, 1, 1),
Content = "Start",
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
Foreground = Brushes.White
};
startButton.Click += Start;
Grid.SetRow(startButton, rowCount);
Grid.SetColumn(startButton, 1);
int colSpan = colCount - 2;
Grid.SetColumnSpan(startButton, colSpan);
//clearButton deklarieren
Button clearButton = new()
{
Name = "clearButton",
Background = Brushes.DodgerBlue,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Padding = new Thickness(1, 1, 1, 1),
Content = new Image
{
Source = new BitmapImage(new Uri("C:\\Users\\myname\\source\\repos\\GameOfLife\\GameOfLifeApp\\ClearButton.png"))
}
};
clearButton.Click += Clear;
Grid.SetRow(clearButton, rowCount);
Grid.SetColumn(clearButton, colCount - 1);
Grid.SetColumnSpan(clearButton, 1);
//Button Array richtig besetzen
int[,] startArray = new int[rowCount, colCount];
int rowLength = startArray.GetLength(0);
int colLength = startArray.GetLength(1);
startArray = Phasen.StartArray(startArray);
buttons = TranslatetoButtonArray(startArray, buttons, rowLength, colLength);
//Funktionsbutton MainGrid hinzufügen
MainGrid.Children.Add(startButton);
MainGrid.Children.Add(clearButton);
MainGrid.Children.Add(resetButton);
}
I have tried setting the heigth and width of the Grid to auto but that doesnt change anything. When i had a fix amount of buttons and declared the Grid aswell as the buttons in the .xaml file there was no such problem.
The reason I think you have this issue is because your code runs after measure and arrange have been done for your window.
I'm not sure why adding the grid doesn't force that again but I have a suggestion could make your problem go away.
Add
Window1.InvalidateVisual();
To the end of your code.
If that still doesn't work then try instead:
Dispatcher.CurrentDispatcher.InvokeAsync(new Action(() =>
{
Window1.InvalidateVisual();
}), DispatcherPriority.ContextIdle);
Still no joy then try not showing whatever this popup is. Maybe you're showing that as modal.
This approach of building everything out in code is inadvisable. Datatemplating is perfectly capable of working fast enough for basic games.

How can I see the changes I make when creating a ControlTemplate?

Maybe I am too ignorant to understand this, but I don't know how I can see changes that I am making when I create ControlTemplate, any ControlTemplate. For example, I'm creating TextBox ControlTemplate
<ControlTemplate x:Name="TextBoxTemplate" x:Key="TemplateForTextBox" TargetType="{x:Type TextBox}" >
<Border Background="#215564">
</Border>
</ControlTemplate>
And I have a Button that dynamically creates a TextBox
void Button_Click_1(object sender, RoutedEventArgs e)
{
var window = new Window();
window.Show();
window.Width = 250;
window.Height = 250;
window.Template = FindResource("WindowControlTemplate1") as ControlTemplate;
dynamicTextBox = new TextBox();
Grid.SetRow(dynamicTextBox, 1);
Grid.SetColumn(dynamicTextBox, 0);
this.TextPanel.Children.Add(dynamicTextBox);
dynamicTextBox.IsReadOnly = false;
dynamicTextBox.Text = "";
//Adjusting margin of text box
Thickness margin = dynamicTextBox.Margin;
margin.Top = 30;
dynamicTextBox.Margin = margin;
margin.Left = 10;
dynamicTextBox.Margin = margin;
margin.Bottom = -25;
dynamicTextBox.Margin = margin;
dynamicTextBox.Template = FindResource("TemplateForTextBox") as ControlTemplate;
}
I can not see TextBox until I(user) press the Button, but I wanna see TextBoxes design while I am writing XAML code(in other words until I press Button)

Dynamically added controls not displaying in wpf page

I am trying to build a Revit plugin using WPF and I am trying to add controls to a window dynamically. However, these controls are not getting displayed in the window and there is no error. Any help would be appreciated. Thank you.
Xaml
<ScrollViewer Margin="0,190,-0.4,-1">
<StackPanel Name="TaskList" Height="auto" Width="auto">
</StackPanel>
</ScrollViewer>
c#
for (var i = 0; i < dt.Rows.Count; i++)
{
Canvas canvas = new Canvas();
canvas.Height = 100;
canvas.Width = 300;
canvas.Background = new SolidColorBrush(Colors.Black);
canvas.Margin = new Thickness(20);
System.Windows.Controls.TextBox tb = new System.Windows.Controls.TextBox();
tb.Background = new SolidColorBrush(Colors.Black);
tb.Foreground = new SolidColorBrush(Colors.White);
tb.Width = 300;
tb.FontSize = 30;
tb.Height = 100;
tb.TextWrapping = TextWrapping.Wrap;
tb.MaxLength = 40;
tb.Text = dt.Rows[i][2].ToString();
canvas.Children.Add(tb);
TaskList.Children.Add(canvas);
TaskList.UpdateLayout();
}
EDIT
I am using a page tag as the base not window. Maybe that changes how i should approach the problem?
Remove Canvas entirely. You can add the TextBox elements to the StackPanel (TaskList) directly. If you want a border or spacing then you should use the Border control, not Canvas.
Additionally, only call UpdateLayout() after you've added all the controls to it:
using System.Windows.Controls;
...
for (var i = 0; i < dt.Rows.Count; i++)
{
TextBox tb = new TextBox();
tb.Background = new SolidColorBrush( Colors.Black );
tb.Foreground = new SolidColorBrush( Colors.White );
tb.Width = 300;
tb.FontSize = 30;
tb.Height = 100;
tb.TextWrapping = TextWrapping.Wrap;
tb.MaxLength = 40;
tb.Text = dt.Rows[i][2].ToString();
this.TaskList.Children.Add( tb );
}
this.TaskList.UpdateLayout();
Consider using an ItemsControl, instead of creating UI elements in code behind:
<ScrollViewer>
<ItemsControl x:Name="TaskList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Width="300" Height="100" FontSize="30"
TextWrapping="Wrap" Text="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
Then just add strings to its Items property:
for (var i = 0; i < dt.Rows.Count; i++)
{
TaskList.Items.Add(dt.Rows[i][2].ToString());
}

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.

WPF grid cells in C# no text

I need to show some data in a structured way with colored letters and rows with background colors.
I made a grid in a WPF Window. It shows the textboxes and some of the labels, but none of the text. Also the column header, last column, gridseperators, grid bot and left edges are invisible.
My grid is called propertiesView.
Code for adding header elements (labels)
private void AddHeaderElement(string text, int row, int col)
{
Label headerElement = new Label();
headerElement.Height = cellHeight;
headerElement.Width = cellWidth;
headerElement.DataContext = text;
headerElement.Background = headerBackground;
headerElement.BorderBrush = new SolidColorBrush(Color.FromRgb(120, 120, 120));
headerElement.BorderThickness = new Thickness(3);
propertiesView.Children.Add(headerElement);
Grid.SetRow(headerElement, row);
Grid.SetColumn(headerElement, col);
}
Code for adding cells
RichTextBox cell = new RichTextBox();
cell.Height = cellHeight;
cell.Width = cellWidth;
cell.ToolTip = toolTip;
cell.DataContext = text;
cell.Background = rowDifferent;
propertiesView.Children.Add(cell);
//box.SetValue(Grid.RowProperty, rowCount);
//box.SetValue(Grid.ColumnProperty, columnCount);
Grid.SetRow(cell, rowCount);
Grid.SetColumn(cell, columnCount);
Code for adding grid seperators
GridSplitter colSeperator = new GridSplitter();
colSeperator.Margin = new Thickness(-2.5, 0, 0, 0);
colSeperator.Width = 5;
colSeperator.ResizeDirection = GridResizeDirection.Columns;
colSeperator.ResizeBehavior = GridResizeBehavior.CurrentAndNext;
colSeperator.VerticalAlignment = VerticalAlignment.Stretch;
colSeperator.HorizontalAlignment = HorizontalAlignment.Left;
propertiesView.Children.Add(colSeperator);
Grid.SetColumn(colSeperator, 0);
Grid.SetRowSpan(colSeperator, totalRows + 1);
The tooltips do show the right text.
I tried using TextBox instead of RichTextBox and setting all this stuff in the class constructor instead of a seperate method.
Well it seems like you just never set the Content dependency property on your labels, or the Document of your RichTextBoxes.
For your labels, as you set the text parameter as the DataContext, you can just add something like
headerElement.SetBinding(Label.ContentProperty, new Binding());
Turns out I needed labels with textblocks, spans and runs.
Style can be added on the span (through properties like Foreground, TextDecoration or FontWeight). Add the text to the span inside a Run, then add all spans to a textblock, which is shown through a label.
Span span = new Span();
span.Foreground = Brushes.Black;
span.Inlines.Add(new Run("Text"));
textBlock.Inlines.Add(span);
Label cell = new Label();
cell.MinHeight = cellHeight;
cell.MaxWidth = cellWidth * 3;
cell.MinWidth = cellWidth;
cell.ToolTip = "toolTip";
cell.BorderThickness = new Thickness(2);
TextBlock cellText = new TextBlock();
cellText.HorizontalAlignment = HorizontalAlignment.Stretch;
cellText.TextWrapping = TextWrapping.WrapWithOverflow;
cell.Content = cellText;
The text works now, I should be able to get the gridseperators working.

Categories

Resources